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/04/29 07:43:55 UTC

[GitHub] [incubator-doris] carlvinhust2012 opened a new pull request, #9311: [feature]add vectorized vjson_scanner

carlvinhust2012 opened a new pull request, #9311:
URL: https://github.com/apache/incubator-doris/pull/9311

   # Proposed changes
   This pr is used to add the vectorized  vjson_scanner, which can support vectorized json import in stream load flow.
   
   Issue Number: close #xxx
   
   ## Problem Summary:
   
   Describe the overview of changes.
   Support the vectorized json import in stream load flow.
   
   ## Checklist(Required)
   
   1. Does it affect the original behavior: (No)
   2. Has unit tests been added: (No)
   3. Has document been added or modified: (No)
   4. Does it need to update dependencies: (No)
   5. 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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r866776817


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,601 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+
+            RETURN_IF_ERROR(filter_block_and_execute_exprs(&output_block, temp_block.get(), slot_num));
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+            
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                auto old_rows = output_block.rows();
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+                _counter->num_rows_unselected += old_rows - output_block.rows();
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    
+    // init file reader
+    RETURN_IF_ERROR(JsonScanner::open_file_reader());
+
+    // init line reader
+    if (_read_json_by_line) {
+        RETURN_IF_ERROR(JsonScanner::open_line_reader());
+    }
+
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    if (range.__isset.jsonpaths) {
+        jsonpath = range.jsonpaths;
+    }
+    if (range.__isset.json_root) {
+        json_root = range.json_root;
+    }
+    if (range.__isset.strip_outer_array) {
+        strip_outer_array = range.strip_outer_array;
+    }
+    if (range.__isset.num_as_string) {
+        num_as_string = range.num_as_string;
+    }
+    if (range.__isset.fuzzy_parse) {
+        fuzzy_parse = range.fuzzy_parse;
+    }
+    
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                               fuzzy_parse, &_scanner_eof, nullptr, _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                                        fuzzy_parse, &_scanner_eof, _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                        bool strip_outer_array, bool num_as_string,bool fuzzy_parse,
+                        bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+               : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                            scanner_eof, file_reader, line_reader),
+                _vhandle_json_callback(nullptr) {
+}
+
+VJsonReader::~VJsonReader() {
+
+}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = JsonReader::_generate_json_paths(jsonpath, &_parsed_jsonpaths);
+        RETURN_IF_ERROR(st);
+    }
+    if (!json_root.empty()) {
+        JsonFunctions::parse_json_paths(json_root, &_parsed_json_root);
+    }
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+    
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns, 
+                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                    bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns, 
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            Status st = _parse_json(is_empty_row, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st);
+            if (*is_empty_row == true && st == Status::OK()) {
+                return Status::OK();
+            }   
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    std::string err_msg("Empty json line");
+                    RETURN_IF_ERROR(_append_error_msg(*_json_doc, err_msg, nullptr));
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) { // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue, std::vector<MutableColumnPtr>& columns,
+                                    const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        std::string err_msg("Expect json object value");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);
+                nullcount++;
+            } else {
+                fmt::memory_buffer error_msg;
+                fmt::format_to(error_msg, "The column `{}` is not nullable, but it's not found in jsondata.", slot_desc->col_name());
+                std::string err_msg = fmt::to_string(error_msg);
+                RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+                break;
+            }
+        }
+    }
+
+    if (nullcount == slot_descs.size()) {
+        std::string err_msg("All fields is null, this is a invalid row.");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+        return Status::OK();
+    }
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_write_data_to_column(rapidjson::Value::ConstValueIterator value, SlotDescriptor* slot_desc,
+                                        vectorized::IColumn* column_ptr, bool* valid) {
+    const char* str_value = nullptr;
+    uint8_t tmp_buf[128] = {0};

Review Comment:
   > Is 128 too big here?
   more is better than less ?



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867802574


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,517 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {}
+
+Status VJsonScanner::get_next(vectorized::Block* output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+    }
+
+    SCOPED_TIMER(_materialize_timer);
+    RETURN_IF_ERROR(BaseScanner::fill_dest_block(output_block, columns));
+
+    *eof = _scanner_eof;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    RETURN_IF_ERROR(JsonScanner::open_based_reader());
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    RETURN_IF_ERROR(JsonScanner::get_range_params(jsonpath, json_root, strip_outer_array,
+                                                  num_as_string, fuzzy_parse));
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof, nullptr,
+                                                _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof,
+                                                _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                         bool strip_outer_array, bool num_as_string, bool fuzzy_parse,
+                         bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+        : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                     scanner_eof, file_reader, line_reader),
+          _vhandle_json_callback(nullptr) {}
+
+VJsonReader::~VJsonReader() {}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // generate _parsed_jsonpaths and _parsed_json_root
+    RETURN_IF_ERROR(JsonReader::_parse_jsonpath_and_json_root(jsonpath, json_root));
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns,
+                                     const std::vector<SlotDescriptor*>& slot_descs,
+                                     bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns,
+                                         const std::vector<SlotDescriptor*>& slot_descs,
+                                         bool* is_empty_row, bool* eof) {

Review Comment:
   we have finished the test, it will take effect.



-- 
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] [incubator-doris] morningman merged pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
morningman merged PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311


-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867344377


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,545 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() {
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status;
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::unique_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);

Review Comment:
   `_materialize_timer` seems not work good here.



-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867678019


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,517 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {}
+
+Status VJsonScanner::get_next(vectorized::Block* output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+    }
+
+    SCOPED_TIMER(_materialize_timer);
+    RETURN_IF_ERROR(BaseScanner::fill_dest_block(output_block, columns));
+
+    *eof = _scanner_eof;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    RETURN_IF_ERROR(JsonScanner::open_based_reader());
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    RETURN_IF_ERROR(JsonScanner::get_range_params(jsonpath, json_root, strip_outer_array,
+                                                  num_as_string, fuzzy_parse));
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof, nullptr,
+                                                _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof,
+                                                _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                         bool strip_outer_array, bool num_as_string, bool fuzzy_parse,
+                         bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+        : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                     scanner_eof, file_reader, line_reader),
+          _vhandle_json_callback(nullptr) {}
+
+VJsonReader::~VJsonReader() {}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // generate _parsed_jsonpaths and _parsed_json_root
+    RETURN_IF_ERROR(JsonReader::_parse_jsonpath_and_json_root(jsonpath, json_root));
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns,
+                                     const std::vector<SlotDescriptor*>& slot_descs,
+                                     bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns,
+                                         const std::vector<SlotDescriptor*>& slot_descs,
+                                         bool* is_empty_row, bool* eof) {

Review Comment:
   does batch_size limit work here?



-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r866597736


##########
be/src/exec/json_scanner.h:
##########
@@ -68,13 +68,13 @@ class JsonScanner : public BaseScanner {
     // Close this scanner
     void close() override;
 
-private:
+protected:
     Status open_file_reader();
     Status open_line_reader();
     Status open_json_reader();
     Status open_next_reader();
 
-private:
+protected:

Review Comment:
   Maybe we can delete some redundant protected



-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867669516


##########
be/src/exec/json_scanner.cpp:
##########
@@ -329,6 +334,19 @@ Status JsonReader::init(const std::string& jsonpath, const std::string& json_roo
     return Status::OK();
 }
 
+Status JsonReader::_parse_jsonpath_and_json_root(const std::string& jsonpath,
+                                                 const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = _generate_json_paths(jsonpath, &_parsed_jsonpaths);

Review Comment:
   code can be simplify here



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867342334


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,545 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() {
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status;
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::unique_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        auto n_columns = 0;
+        for (const auto slot_desc : _src_slot_descs) {
+            temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                     slot_desc->get_data_type_ptr(),
+                                                     slot_desc->col_name()));
+        }
+
+        RETURN_IF_ERROR(BaseScanner::filter_block(temp_block.get(), slot_num));

Review Comment:
   > should update _counter->num_rows_unselected ?
   
   yes, has update in BaseScanner::filter_block



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867792394


##########
be/src/vec/exec/vbroker_scan_node.h:
##########
@@ -31,7 +31,8 @@ namespace vectorized {
 class VBrokerScanNode final : public BrokerScanNode {
 public:
     VBrokerScanNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs);
-    ~VBrokerScanNode() override = default;
+
+    ~VBrokerScanNode() { close(_runtime_state); }

Review Comment:
   it confirmed to be removed.



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867341957


##########
be/src/exec/base_scanner.cpp:
##########
@@ -284,6 +330,12 @@ void BaseScanner::close() {
     if (!_pre_filter_ctxs.empty()) {
         Expr::close(_pre_filter_ctxs, _state);
     }
+
+    if (_state != nullptr && _state->enable_vectorized_exec()) {

Review Comment:
   > no need `_state != nullptr`?
   it just use to protect the _state when another UT pass a nullptr.
   



-- 
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] [incubator-doris] github-actions[bot] commented on pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#issuecomment-1121963868

   PR approved by anyone and no changes requested.


-- 
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] [incubator-doris] HappenLee commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r862598988


##########
be/src/vec/exec/vjson_scanner.h:
##########
@@ -0,0 +1,158 @@
+// 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.
+
+#ifndef BE_SRC_VJSON_SCANNER_H_
+#define BE_SRC_VJSON_SCANNER_H_
+
+#include <rapidjson/document.h>
+#include <rapidjson/error/en.h>
+#include <rapidjson/stringbuffer.h>
+#include <rapidjson/writer.h>
+
+#include <map>
+#include <memory>
+#include <sstream>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include "common/status.h"
+#include "exec/base_scanner.h"
+#include "exec/json_scanner.h"
+#include "exec/exec_node.h"
+#include "runtime/row_batch.h"
+#include "runtime/descriptors.h"
+#include "runtime/mem_pool.h"
+#include "runtime/tuple.h"
+#include "runtime/mem_tracker.h"
+#include "util/runtime_profile.h"
+#include "exprs/expr_context.h"
+
+namespace doris {
+class ExprContext;
+
+namespace vectorized {
+class VJsonReader;
+
+class VJsonScanner : public JsonScanner {
+public:
+    VJsonScanner(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);
+        
+    ~VJsonScanner();
+    
+    Status open() override;
+
+    void close() override;
+
+    // Status get_next(std::vector<MutableColumnPtr>& columns, bool* eof) override;
+    Status get_next(vectorized::Block& output_block, bool* eof) override;
+
+private:
+    Status open_vjson_reader();
+    Status open_next_reader();
+
+private:
+    const std::vector<TBrokerRangeDesc>& _ranges;
+    const std::vector<TNetworkAddress>& _broker_addresses;
+
+    // Reader
+    FileReader* _cur_file_reader;

Review Comment:
   This is a bad design. There is a member variable with the same name in the parent-child class. You should reuse it instead of using it with private



-- 
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] [incubator-doris] carlvinhust2012 commented on pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#issuecomment-1123109074

   > @carlvinhust2012 please rebase to solve the conflict
   
   ok,I will be rebase the code.


-- 
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] [incubator-doris] xy720 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
xy720 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r862481511


##########
be/src/exec/base_scanner.h:
##########
@@ -65,6 +72,10 @@ class BaseScanner {
     virtual Status get_next(std::vector<vectorized::MutableColumnPtr>& columns, bool* eof) {
         return Status::NotSupported("Not Implemented get block");
     }
+    
+    virtual Status get_next(vectorized::Block& output_block, bool* eof) {

Review Comment:
   I only see it be implemented in vJsonScanner. May be you also need to implement it in vBrokerScanner?



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867336069


##########
be/src/exec/base_scanner.cpp:
##########
@@ -260,6 +279,33 @@ Status BaseScanner::_fill_dest_tuple(Tuple* dest_tuple, MemPool* mem_pool) {
     return Status::OK();
 }
 
+Status BaseScanner::filter_block(vectorized::Block* temp_block, size_t slot_num) {
+    // filter src tuple by preceding filter first
+    auto old_rows = temp_block->rows();

Review Comment:
   it should update "old_rows" in the for loop.



-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867344791


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,545 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() {
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status;
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::unique_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        auto n_columns = 0;
+        for (const auto slot_desc : _src_slot_descs) {
+            temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                     slot_desc->get_data_type_ptr(),
+                                                     slot_desc->col_name()));
+        }
+
+        RETURN_IF_ERROR(BaseScanner::filter_block(temp_block.get(), slot_num));
+
+        if (_dest_vexpr_ctx.empty()) {
+            output_block = *(temp_block.get());
+        } else {
+            RETURN_IF_ERROR(BaseScanner::execute_exprs(&output_block, temp_block.get()));

Review Comment:
   seems have many `redundant-smartptr-get` here.
   https://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-smartptr-get.html



-- 
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] [incubator-doris] HappenLee commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r868766116


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,521 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {}
+
+Status VJsonScanner::get_next(vectorized::Block* output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);

Review Comment:
   no need update counter 1 in while loop. only after the while, use column->size()



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r866623725


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,729 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _ranges(ranges),
+          _broker_addresses(broker_addresses),
+          _cur_file_reader(nullptr),
+          _cur_line_reader(nullptr),
+          _cur_vjson_reader(nullptr),
+          _next_range(0),
+          _cur_reader_eof(false),
+          _read_json_by_line(false) { 
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status = Status::OK();
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+            }
+
+            // Do vectorized expr here to speed up load
+            output_block = VExprContext::get_output_block_after_execute_exprs(_dest_vexpr_ctx,
+                             *(temp_block.get()), status);
+            if (UNLIKELY(output_block.rows() == 0)) {
+                return status;
+            }
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+            
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) { 
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    
+    // init the file reader
+    RETURN_IF_ERROR(JsonScanner::open_file_reader());
+    _cur_file_reader = JsonScanner::get_cur_file_reader();
+    _cur_reader_eof = JsonScanner::get_cur_reader_eof();
+    _read_json_by_line = JsonScanner::get_read_json_by_line();
+
+    // init line reader
+    if (_read_json_by_line) {
+        RETURN_IF_ERROR(JsonScanner::open_line_reader());
+        _cur_line_reader = JsonScanner::get_cur_line_reader();
+        _cur_reader_eof = JsonScanner::get_cur_reader_eof();
+        _skip_next_line = JsonScanner::get_skip_next_line();
+    }
+
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    if (range.__isset.jsonpaths) {
+        jsonpath = range.jsonpaths;
+    }
+    if (range.__isset.json_root) {
+        json_root = range.json_root;
+    }
+    if (range.__isset.strip_outer_array) {
+        strip_outer_array = range.strip_outer_array;
+    }
+    if (range.__isset.num_as_string) {
+        num_as_string = range.num_as_string;
+    }
+    if (range.__isset.fuzzy_parse) {
+        fuzzy_parse = range.fuzzy_parse;
+    }
+    
+    if (_read_json_by_line) {
+        _cur_vjson_reader = new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                               fuzzy_parse, &_scanner_eof, nullptr, _cur_line_reader);
+    } else {
+        _cur_vjson_reader =  new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                                        fuzzy_parse, &_scanner_eof, _cur_file_reader);
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                        bool strip_outer_array, bool num_as_string,bool fuzzy_parse,
+                        bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+               : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                            scanner_eof, file_reader, line_reader),
+                _vhandle_json_callback(nullptr),
+                _next_line(0),
+                _total_lines(0),
+                _state(state),
+                _counter(counter),
+                _profile(profile),
+                _strip_outer_array(strip_outer_array),
+                _fuzzy_parse(fuzzy_parse),
+                _json_doc(nullptr),
+                _scanner_eof(scanner_eof) {
+}
+
+VJsonReader::~VJsonReader() {
+
+}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = JsonReader::_generate_json_paths(jsonpath, &_parsed_jsonpaths);
+        RETURN_IF_ERROR(st);
+    }
+    if (!json_root.empty()) {
+        JsonFunctions::parse_json_paths(json_root, &_parsed_json_root);
+    }
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+    
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns, 
+                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                    bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns, 
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            size_t size = 0;
+            Status st = JsonReader::_parse_json_doc(&size, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st); // terminate if encounter other errors
+            if (size == 0 || *eof) {          // read all data, then return
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            _json_doc = VJsonReader::get_json_doc();
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(*_json_doc); },
+                            [&]() -> std::string { return "Empty json line"; }, _scanner_eof));
+                    _counter->num_rows_filtered++;
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) { // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue, std::vector<MutableColumnPtr>& columns,
+                                    const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
+                [&]() -> std::string { return "Expect json object value"; }, _scanner_eof));
+        _counter->num_rows_filtered++;
+        *valid = false; // current row is invalid
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);
+                nullcount++;
+                // LOG(INFO) << "not found in objectValue";
+            } else {
+               RETURN_IF_ERROR( _state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
+                        [&]() -> std::string {
+                        fmt::memory_buffer error_msg;
+                        fmt::format_to(error_msg, "The column `{}` is not nullable, but it's not found in jsondata.", slot_desc->col_name());
+                        return fmt::to_string(error_msg);
+                        }, _scanner_eof));
+                _counter->num_rows_filtered++;
+                *valid = false; // current row is invalid
+                break;
+            }
+        }
+    }
+
+    if (nullcount == slot_descs.size()) {
+        RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
+                [&]() -> std::string { return "All fields is null, this is a invalid row."; }, _scanner_eof));
+        _counter->num_rows_filtered++;
+        *valid = false;
+        return Status::OK();
+    }
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_write_data_to_column(rapidjson::Value::ConstValueIterator value, SlotDescriptor* slot_desc,
+                                        vectorized::IColumn* column_ptr, bool* valid) {
+    const char* str_value = nullptr;
+    uint8_t tmp_buf[128] = {0};
+    int32_t wbytes = 0;
+    
+    if (slot_desc->is_nullable()) {
+        auto* nullable_column =
+            reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+        nullable_column->get_null_map_data().push_back(0);
+        column_ptr = &nullable_column->get_nested_column();
+    }
+
+    switch (value->GetType()) {
+    case rapidjson::Type::kStringType:
+        str_value = value->GetString();
+        wbytes = strlen(str_value);
+        break;
+    case rapidjson::Type::kNumberType:
+        if (value->IsUint()) { 
+            wbytes = sprintf((char *)tmp_buf, "%u", value->GetUint());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsInt()) {
+            wbytes = sprintf((char *)tmp_buf, "%d", value->GetInt());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsUint64()) {
+            wbytes = sprintf((char *)tmp_buf, "%lu", value->GetUint64());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsInt64()) {
+            wbytes = sprintf((char *)tmp_buf, "%ld", value->GetInt64());
+            str_value = (char *)tmp_buf;
+        } else {
+            wbytes = sprintf((char *)tmp_buf, "%f", value->GetDouble());
+            str_value = (char *)tmp_buf;
+        }
+        break;
+    case rapidjson::Type::kFalseType:
+        wbytes = 1;
+        str_value = (char *)"0";
+        break;
+    case rapidjson::Type::kTrueType:
+        wbytes = 1;
+        str_value = (char *)"1";
+        break;
+    case rapidjson::Type::kNullType:
+        if (slot_desc->is_nullable()) {
+            auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+            nullable_column->insert_data(nullptr, 0);
+        } else {
+            RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(*value); },
+                    [&]() -> std::string {
+                    fmt::memory_buffer error_msg;
+                    fmt::format_to(error_msg, "Json value is null, but the column `{}` is not nullable.", slot_desc->col_name());
+                    return fmt::to_string(error_msg);
+                    }, _scanner_eof));
+            _counter->num_rows_filtered++;
+            *valid = false;
+            return Status::OK();
+        }
+        break;
+    default:
+        // for other type like array or object. we convert it to string to save
+        std::string json_str = JsonReader::_print_json_value(*value);
+        wbytes = json_str.size();
+        str_value = json_str.c_str();
+        break;
+    }
+    
+    RETURN_IF_ERROR(_insert_to_column(column_ptr, slot_desc, str_value, wbytes));
+
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_insert_to_column(vectorized::IColumn* column_ptr, SlotDescriptor* slot_desc,
+                                    const char* value_ptr, int32_t& wbytes) {
+    switch (slot_desc->type().type) {

Review Comment:
   yes, VExpr just can process the input of which type is varchar



-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867345067


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,545 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() {
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status;
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::unique_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        auto n_columns = 0;
+        for (const auto slot_desc : _src_slot_descs) {
+            temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                     slot_desc->get_data_type_ptr(),
+                                                     slot_desc->col_name()));
+        }
+
+        RETURN_IF_ERROR(BaseScanner::filter_block(temp_block.get(), slot_num));
+
+        if (_dest_vexpr_ctx.empty()) {
+            output_block = *(temp_block.get());
+        } else {
+            RETURN_IF_ERROR(BaseScanner::execute_exprs(&output_block, temp_block.get()));
+        }
+    }
+
+    *eof = _scanner_eof;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    RETURN_IF_ERROR(JsonScanner::open_based_reader());
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    RETURN_IF_ERROR(JsonScanner::get_range_params(jsonpath, json_root, strip_outer_array,
+                                                  num_as_string, fuzzy_parse));
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof, nullptr,
+                                                _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof,
+                                                _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                         bool strip_outer_array, bool num_as_string, bool fuzzy_parse,
+                         bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+        : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                     scanner_eof, file_reader, line_reader),
+          _vhandle_json_callback(nullptr) {}
+
+VJsonReader::~VJsonReader() {}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // generate _parsed_jsonpaths and _parsed_json_root
+    RETURN_IF_ERROR(JsonReader::_parse_jsonpath_and_json_root(jsonpath, json_root));
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns,
+                                     const std::vector<SlotDescriptor*>& slot_descs,
+                                     bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns,
+                                         const std::vector<SlotDescriptor*>& slot_descs,
+                                         bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            Status st = _parse_json(is_empty_row, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st);
+            if (*is_empty_row == true && st == Status::OK()) {
+                return Status::OK();
+            }
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    RETURN_IF_ERROR(_append_error_msg(*_json_doc, "Empty json line", "", nullptr));
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) {                                   // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue,
+                                      std::vector<MutableColumnPtr>& columns,
+                                      const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        RETURN_IF_ERROR(_append_error_msg(objectValue, "Expect json object value", "", valid));
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);

Review Comment:
   if you want set null, better use `insert_default`



-- 
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] [incubator-doris] HappenLee commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867311008


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,593 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() {
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                         slot_desc->get_data_type_ptr(),
+                                                         slot_desc->col_name()));
+            }
+
+            RETURN_IF_ERROR(
+                    filter_block_and_execute_exprs(&output_block, temp_block.get(), slot_num));
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                          slot_desc->get_data_type_ptr(),
+                                                          slot_desc->col_name()));
+            }
+
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    auto old_rows = output_block.rows();
+                    RETURN_IF_ERROR(
+                            VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                    _counter->num_rows_unselected += old_rows - output_block.rows();
+                }
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }

Review Comment:
   *eof = _scanner_eof



-- 
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] [incubator-doris] morningman commented on pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
morningman commented on PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#issuecomment-1123086261

   @carlvinhust2012 please rebase to solve the conflict


-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867724517


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,517 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {}
+
+Status VJsonScanner::get_next(vectorized::Block* output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+    }
+
+    SCOPED_TIMER(_materialize_timer);
+    RETURN_IF_ERROR(BaseScanner::fill_dest_block(output_block, columns));
+
+    *eof = _scanner_eof;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    RETURN_IF_ERROR(JsonScanner::open_based_reader());
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    RETURN_IF_ERROR(JsonScanner::get_range_params(jsonpath, json_root, strip_outer_array,
+                                                  num_as_string, fuzzy_parse));
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof, nullptr,
+                                                _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof,
+                                                _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                         bool strip_outer_array, bool num_as_string, bool fuzzy_parse,
+                         bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+        : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                     scanner_eof, file_reader, line_reader),
+          _vhandle_json_callback(nullptr) {}
+
+VJsonReader::~VJsonReader() {}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // generate _parsed_jsonpaths and _parsed_json_root
+    RETURN_IF_ERROR(JsonReader::_parse_jsonpath_and_json_root(jsonpath, json_root));
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns,
+                                     const std::vector<SlotDescriptor*>& slot_descs,
+                                     bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns,
+                                         const std::vector<SlotDescriptor*>& slot_descs,
+                                         bool* is_empty_row, bool* eof) {

Review Comment:
   It seems not work good when _json_doc is array type



-- 
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] [incubator-doris] HappenLee commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r862599417


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,729 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _ranges(ranges),
+          _broker_addresses(broker_addresses),
+          _cur_file_reader(nullptr),
+          _cur_line_reader(nullptr),
+          _cur_vjson_reader(nullptr),
+          _next_range(0),
+          _cur_reader_eof(false),
+          _read_json_by_line(false) { 
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+    if (_cur_vjson_reader != nullptr) {

Review Comment:
   should use `unique_ptr`



##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,729 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _ranges(ranges),
+          _broker_addresses(broker_addresses),
+          _cur_file_reader(nullptr),
+          _cur_line_reader(nullptr),
+          _cur_vjson_reader(nullptr),
+          _next_range(0),
+          _cur_reader_eof(false),
+          _read_json_by_line(false) { 
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status = Status::OK();
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+            }
+
+            // Do vectorized expr here to speed up load
+            output_block = VExprContext::get_output_block_after_execute_exprs(_dest_vexpr_ctx,
+                             *(temp_block.get()), status);
+            if (UNLIKELY(output_block.rows() == 0)) {
+                return status;
+            }
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+            
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) { 
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    
+    // init the file reader
+    RETURN_IF_ERROR(JsonScanner::open_file_reader());
+    _cur_file_reader = JsonScanner::get_cur_file_reader();
+    _cur_reader_eof = JsonScanner::get_cur_reader_eof();
+    _read_json_by_line = JsonScanner::get_read_json_by_line();
+
+    // init line reader
+    if (_read_json_by_line) {
+        RETURN_IF_ERROR(JsonScanner::open_line_reader());
+        _cur_line_reader = JsonScanner::get_cur_line_reader();
+        _cur_reader_eof = JsonScanner::get_cur_reader_eof();
+        _skip_next_line = JsonScanner::get_skip_next_line();
+    }
+
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    if (range.__isset.jsonpaths) {
+        jsonpath = range.jsonpaths;
+    }
+    if (range.__isset.json_root) {
+        json_root = range.json_root;
+    }
+    if (range.__isset.strip_outer_array) {
+        strip_outer_array = range.strip_outer_array;
+    }
+    if (range.__isset.num_as_string) {
+        num_as_string = range.num_as_string;
+    }
+    if (range.__isset.fuzzy_parse) {
+        fuzzy_parse = range.fuzzy_parse;
+    }
+    
+    if (_read_json_by_line) {
+        _cur_vjson_reader = new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                               fuzzy_parse, &_scanner_eof, nullptr, _cur_line_reader);
+    } else {
+        _cur_vjson_reader =  new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                                        fuzzy_parse, &_scanner_eof, _cur_file_reader);
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                        bool strip_outer_array, bool num_as_string,bool fuzzy_parse,
+                        bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+               : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                            scanner_eof, file_reader, line_reader),
+                _vhandle_json_callback(nullptr),
+                _next_line(0),
+                _total_lines(0),
+                _state(state),
+                _counter(counter),
+                _profile(profile),
+                _strip_outer_array(strip_outer_array),
+                _fuzzy_parse(fuzzy_parse),
+                _json_doc(nullptr),
+                _scanner_eof(scanner_eof) {
+}
+
+VJsonReader::~VJsonReader() {
+
+}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = JsonReader::_generate_json_paths(jsonpath, &_parsed_jsonpaths);
+        RETURN_IF_ERROR(st);
+    }
+    if (!json_root.empty()) {
+        JsonFunctions::parse_json_paths(json_root, &_parsed_json_root);
+    }
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+    
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns, 
+                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                    bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns, 
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            size_t size = 0;
+            Status st = JsonReader::_parse_json_doc(&size, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st); // terminate if encounter other errors
+            if (size == 0 || *eof) {          // read all data, then return
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            _json_doc = VJsonReader::get_json_doc();
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(*_json_doc); },
+                            [&]() -> std::string { return "Empty json line"; }, _scanner_eof));
+                    _counter->num_rows_filtered++;
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) { // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue, std::vector<MutableColumnPtr>& columns,
+                                    const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
+                [&]() -> std::string { return "Expect json object value"; }, _scanner_eof));
+        _counter->num_rows_filtered++;
+        *valid = false; // current row is invalid
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);
+                nullcount++;
+                // LOG(INFO) << "not found in objectValue";
+            } else {
+               RETURN_IF_ERROR( _state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
+                        [&]() -> std::string {
+                        fmt::memory_buffer error_msg;
+                        fmt::format_to(error_msg, "The column `{}` is not nullable, but it's not found in jsondata.", slot_desc->col_name());
+                        return fmt::to_string(error_msg);
+                        }, _scanner_eof));
+                _counter->num_rows_filtered++;
+                *valid = false; // current row is invalid
+                break;
+            }
+        }
+    }
+
+    if (nullcount == slot_descs.size()) {
+        RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
+                [&]() -> std::string { return "All fields is null, this is a invalid row."; }, _scanner_eof));
+        _counter->num_rows_filtered++;
+        *valid = false;
+        return Status::OK();
+    }
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_write_data_to_column(rapidjson::Value::ConstValueIterator value, SlotDescriptor* slot_desc,
+                                        vectorized::IColumn* column_ptr, bool* valid) {
+    const char* str_value = nullptr;
+    uint8_t tmp_buf[128] = {0};
+    int32_t wbytes = 0;
+    
+    if (slot_desc->is_nullable()) {
+        auto* nullable_column =
+            reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+        nullable_column->get_null_map_data().push_back(0);
+        column_ptr = &nullable_column->get_nested_column();
+    }
+
+    switch (value->GetType()) {
+    case rapidjson::Type::kStringType:
+        str_value = value->GetString();
+        wbytes = strlen(str_value);
+        break;
+    case rapidjson::Type::kNumberType:
+        if (value->IsUint()) { 
+            wbytes = sprintf((char *)tmp_buf, "%u", value->GetUint());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsInt()) {
+            wbytes = sprintf((char *)tmp_buf, "%d", value->GetInt());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsUint64()) {
+            wbytes = sprintf((char *)tmp_buf, "%lu", value->GetUint64());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsInt64()) {
+            wbytes = sprintf((char *)tmp_buf, "%ld", value->GetInt64());
+            str_value = (char *)tmp_buf;
+        } else {
+            wbytes = sprintf((char *)tmp_buf, "%f", value->GetDouble());
+            str_value = (char *)tmp_buf;
+        }
+        break;
+    case rapidjson::Type::kFalseType:
+        wbytes = 1;
+        str_value = (char *)"0";
+        break;
+    case rapidjson::Type::kTrueType:
+        wbytes = 1;
+        str_value = (char *)"1";
+        break;
+    case rapidjson::Type::kNullType:
+        if (slot_desc->is_nullable()) {
+            auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+            nullable_column->insert_data(nullptr, 0);
+        } else {
+            RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(*value); },
+                    [&]() -> std::string {
+                    fmt::memory_buffer error_msg;
+                    fmt::format_to(error_msg, "Json value is null, but the column `{}` is not nullable.", slot_desc->col_name());
+                    return fmt::to_string(error_msg);
+                    }, _scanner_eof));
+            _counter->num_rows_filtered++;
+            *valid = false;
+            return Status::OK();
+        }
+        break;
+    default:
+        // for other type like array or object. we convert it to string to save
+        std::string json_str = JsonReader::_print_json_value(*value);
+        wbytes = json_str.size();
+        str_value = json_str.c_str();
+        break;
+    }
+    
+    RETURN_IF_ERROR(_insert_to_column(column_ptr, slot_desc, str_value, wbytes));
+
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_insert_to_column(vectorized::IColumn* column_ptr, SlotDescriptor* slot_desc,
+                                    const char* value_ptr, int32_t& wbytes) {
+    switch (slot_desc->type().type) {

Review Comment:
   the type seems always is varchar ? 



##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,729 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _ranges(ranges),
+          _broker_addresses(broker_addresses),
+          _cur_file_reader(nullptr),
+          _cur_line_reader(nullptr),
+          _cur_vjson_reader(nullptr),
+          _next_range(0),
+          _cur_reader_eof(false),
+          _read_json_by_line(false) { 
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status = Status::OK();
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {

Review Comment:
   This part of the logic for filtering and executing expressions should be abstracted to the base scanner so that other scanners can use it instead of writing it once



##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,729 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _ranges(ranges),
+          _broker_addresses(broker_addresses),
+          _cur_file_reader(nullptr),
+          _cur_line_reader(nullptr),
+          _cur_vjson_reader(nullptr),
+          _next_range(0),
+          _cur_reader_eof(false),
+          _read_json_by_line(false) { 
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status = Status::OK();
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+            }
+
+            // Do vectorized expr here to speed up load
+            output_block = VExprContext::get_output_block_after_execute_exprs(_dest_vexpr_ctx,
+                             *(temp_block.get()), status);
+            if (UNLIKELY(output_block.rows() == 0)) {
+                return status;
+            }
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+            
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) { 
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    
+    // init the file reader
+    RETURN_IF_ERROR(JsonScanner::open_file_reader());
+    _cur_file_reader = JsonScanner::get_cur_file_reader();
+    _cur_reader_eof = JsonScanner::get_cur_reader_eof();
+    _read_json_by_line = JsonScanner::get_read_json_by_line();
+
+    // init line reader
+    if (_read_json_by_line) {
+        RETURN_IF_ERROR(JsonScanner::open_line_reader());
+        _cur_line_reader = JsonScanner::get_cur_line_reader();
+        _cur_reader_eof = JsonScanner::get_cur_reader_eof();
+        _skip_next_line = JsonScanner::get_skip_next_line();
+    }
+
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    if (range.__isset.jsonpaths) {
+        jsonpath = range.jsonpaths;
+    }
+    if (range.__isset.json_root) {
+        json_root = range.json_root;
+    }
+    if (range.__isset.strip_outer_array) {
+        strip_outer_array = range.strip_outer_array;
+    }
+    if (range.__isset.num_as_string) {
+        num_as_string = range.num_as_string;
+    }
+    if (range.__isset.fuzzy_parse) {
+        fuzzy_parse = range.fuzzy_parse;
+    }
+    
+    if (_read_json_by_line) {
+        _cur_vjson_reader = new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                               fuzzy_parse, &_scanner_eof, nullptr, _cur_line_reader);
+    } else {
+        _cur_vjson_reader =  new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                                        fuzzy_parse, &_scanner_eof, _cur_file_reader);
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                        bool strip_outer_array, bool num_as_string,bool fuzzy_parse,
+                        bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+               : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                            scanner_eof, file_reader, line_reader),
+                _vhandle_json_callback(nullptr),
+                _next_line(0),
+                _total_lines(0),
+                _state(state),
+                _counter(counter),
+                _profile(profile),
+                _strip_outer_array(strip_outer_array),
+                _fuzzy_parse(fuzzy_parse),
+                _json_doc(nullptr),
+                _scanner_eof(scanner_eof) {
+}
+
+VJsonReader::~VJsonReader() {
+
+}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = JsonReader::_generate_json_paths(jsonpath, &_parsed_jsonpaths);
+        RETURN_IF_ERROR(st);
+    }
+    if (!json_root.empty()) {
+        JsonFunctions::parse_json_paths(json_root, &_parsed_json_root);
+    }
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+    
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns, 
+                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                    bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns, 
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            size_t size = 0;
+            Status st = JsonReader::_parse_json_doc(&size, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st); // terminate if encounter other errors
+            if (size == 0 || *eof) {          // read all data, then return
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            _json_doc = VJsonReader::get_json_doc();
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(*_json_doc); },
+                            [&]() -> std::string { return "Empty json line"; }, _scanner_eof));
+                    _counter->num_rows_filtered++;
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) { // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue, std::vector<MutableColumnPtr>& columns,
+                                    const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },

Review Comment:
   ```
           RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
                   [&]() -> std::string { return "Expect json object value"; }, _scanner_eof));
           _counter->num_rows_filtered++;
           *valid = false; // current row is invalid
   ```
   should use replace the 3 line use a function or lambad function



##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,729 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _ranges(ranges),
+          _broker_addresses(broker_addresses),
+          _cur_file_reader(nullptr),
+          _cur_line_reader(nullptr),
+          _cur_vjson_reader(nullptr),
+          _next_range(0),
+          _cur_reader_eof(false),
+          _read_json_by_line(false) { 
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status = Status::OK();
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+            }
+
+            // Do vectorized expr here to speed up load
+            output_block = VExprContext::get_output_block_after_execute_exprs(_dest_vexpr_ctx,
+                             *(temp_block.get()), status);
+            if (UNLIKELY(output_block.rows() == 0)) {
+                return status;
+            }
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+            
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) { 
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    
+    // init the file reader
+    RETURN_IF_ERROR(JsonScanner::open_file_reader());
+    _cur_file_reader = JsonScanner::get_cur_file_reader();
+    _cur_reader_eof = JsonScanner::get_cur_reader_eof();
+    _read_json_by_line = JsonScanner::get_read_json_by_line();
+
+    // init line reader
+    if (_read_json_by_line) {
+        RETURN_IF_ERROR(JsonScanner::open_line_reader());
+        _cur_line_reader = JsonScanner::get_cur_line_reader();
+        _cur_reader_eof = JsonScanner::get_cur_reader_eof();
+        _skip_next_line = JsonScanner::get_skip_next_line();
+    }
+
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    if (range.__isset.jsonpaths) {
+        jsonpath = range.jsonpaths;
+    }
+    if (range.__isset.json_root) {
+        json_root = range.json_root;
+    }
+    if (range.__isset.strip_outer_array) {
+        strip_outer_array = range.strip_outer_array;
+    }
+    if (range.__isset.num_as_string) {
+        num_as_string = range.num_as_string;
+    }
+    if (range.__isset.fuzzy_parse) {
+        fuzzy_parse = range.fuzzy_parse;
+    }
+    
+    if (_read_json_by_line) {
+        _cur_vjson_reader = new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                               fuzzy_parse, &_scanner_eof, nullptr, _cur_line_reader);
+    } else {
+        _cur_vjson_reader =  new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                                        fuzzy_parse, &_scanner_eof, _cur_file_reader);
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                        bool strip_outer_array, bool num_as_string,bool fuzzy_parse,
+                        bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+               : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                            scanner_eof, file_reader, line_reader),
+                _vhandle_json_callback(nullptr),
+                _next_line(0),
+                _total_lines(0),
+                _state(state),
+                _counter(counter),
+                _profile(profile),
+                _strip_outer_array(strip_outer_array),
+                _fuzzy_parse(fuzzy_parse),
+                _json_doc(nullptr),
+                _scanner_eof(scanner_eof) {
+}
+
+VJsonReader::~VJsonReader() {
+
+}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = JsonReader::_generate_json_paths(jsonpath, &_parsed_jsonpaths);
+        RETURN_IF_ERROR(st);
+    }
+    if (!json_root.empty()) {
+        JsonFunctions::parse_json_paths(json_root, &_parsed_json_root);
+    }
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+    
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns, 
+                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                    bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns, 
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            size_t size = 0;
+            Status st = JsonReader::_parse_json_doc(&size, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st); // terminate if encounter other errors
+            if (size == 0 || *eof) {          // read all data, then return
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            _json_doc = VJsonReader::get_json_doc();
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(*_json_doc); },
+                            [&]() -> std::string { return "Empty json line"; }, _scanner_eof));
+                    _counter->num_rows_filtered++;
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) { // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue, std::vector<MutableColumnPtr>& columns,
+                                    const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
+                [&]() -> std::string { return "Expect json object value"; }, _scanner_eof));
+        _counter->num_rows_filtered++;
+        *valid = false; // current row is invalid
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);
+                nullcount++;
+                // LOG(INFO) << "not found in objectValue";

Review Comment:
   delete the useless code



##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,729 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _ranges(ranges),
+          _broker_addresses(broker_addresses),
+          _cur_file_reader(nullptr),
+          _cur_line_reader(nullptr),
+          _cur_vjson_reader(nullptr),
+          _next_range(0),
+          _cur_reader_eof(false),
+          _read_json_by_line(false) { 
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status = Status::OK();
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+            }
+
+            // Do vectorized expr here to speed up load
+            output_block = VExprContext::get_output_block_after_execute_exprs(_dest_vexpr_ctx,
+                             *(temp_block.get()), status);
+            if (UNLIKELY(output_block.rows() == 0)) {
+                return status;
+            }
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+            
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) { 
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    
+    // init the file reader
+    RETURN_IF_ERROR(JsonScanner::open_file_reader());
+    _cur_file_reader = JsonScanner::get_cur_file_reader();
+    _cur_reader_eof = JsonScanner::get_cur_reader_eof();
+    _read_json_by_line = JsonScanner::get_read_json_by_line();
+
+    // init line reader
+    if (_read_json_by_line) {
+        RETURN_IF_ERROR(JsonScanner::open_line_reader());
+        _cur_line_reader = JsonScanner::get_cur_line_reader();
+        _cur_reader_eof = JsonScanner::get_cur_reader_eof();
+        _skip_next_line = JsonScanner::get_skip_next_line();
+    }
+
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        delete _cur_vjson_reader;
+        _cur_vjson_reader = nullptr;
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    if (range.__isset.jsonpaths) {
+        jsonpath = range.jsonpaths;
+    }
+    if (range.__isset.json_root) {
+        json_root = range.json_root;
+    }
+    if (range.__isset.strip_outer_array) {
+        strip_outer_array = range.strip_outer_array;
+    }
+    if (range.__isset.num_as_string) {
+        num_as_string = range.num_as_string;
+    }
+    if (range.__isset.fuzzy_parse) {
+        fuzzy_parse = range.fuzzy_parse;
+    }
+    
+    if (_read_json_by_line) {
+        _cur_vjson_reader = new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                               fuzzy_parse, &_scanner_eof, nullptr, _cur_line_reader);
+    } else {
+        _cur_vjson_reader =  new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                                        fuzzy_parse, &_scanner_eof, _cur_file_reader);
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                        bool strip_outer_array, bool num_as_string,bool fuzzy_parse,
+                        bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+               : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                            scanner_eof, file_reader, line_reader),
+                _vhandle_json_callback(nullptr),
+                _next_line(0),
+                _total_lines(0),
+                _state(state),
+                _counter(counter),
+                _profile(profile),
+                _strip_outer_array(strip_outer_array),
+                _fuzzy_parse(fuzzy_parse),
+                _json_doc(nullptr),
+                _scanner_eof(scanner_eof) {
+}
+
+VJsonReader::~VJsonReader() {
+
+}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = JsonReader::_generate_json_paths(jsonpath, &_parsed_jsonpaths);
+        RETURN_IF_ERROR(st);
+    }
+    if (!json_root.empty()) {
+        JsonFunctions::parse_json_paths(json_root, &_parsed_json_root);
+    }
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+    
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns, 
+                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                    bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns, 
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            size_t size = 0;
+            Status st = JsonReader::_parse_json_doc(&size, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st); // terminate if encounter other errors
+            if (size == 0 || *eof) {          // read all data, then return
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            _json_doc = VJsonReader::get_json_doc();
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(*_json_doc); },
+                            [&]() -> std::string { return "Empty json line"; }, _scanner_eof));
+                    _counter->num_rows_filtered++;
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) { // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue, std::vector<MutableColumnPtr>& columns,
+                                    const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
+                [&]() -> std::string { return "Expect json object value"; }, _scanner_eof));
+        _counter->num_rows_filtered++;
+        *valid = false; // current row is invalid
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);
+                nullcount++;
+                // LOG(INFO) << "not found in objectValue";
+            } else {
+               RETURN_IF_ERROR( _state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
+                        [&]() -> std::string {
+                        fmt::memory_buffer error_msg;
+                        fmt::format_to(error_msg, "The column `{}` is not nullable, but it's not found in jsondata.", slot_desc->col_name());
+                        return fmt::to_string(error_msg);
+                        }, _scanner_eof));
+                _counter->num_rows_filtered++;
+                *valid = false; // current row is invalid
+                break;
+            }
+        }
+    }
+
+    if (nullcount == slot_descs.size()) {
+        RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(objectValue); },
+                [&]() -> std::string { return "All fields is null, this is a invalid row."; }, _scanner_eof));
+        _counter->num_rows_filtered++;
+        *valid = false;
+        return Status::OK();
+    }
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_write_data_to_column(rapidjson::Value::ConstValueIterator value, SlotDescriptor* slot_desc,
+                                        vectorized::IColumn* column_ptr, bool* valid) {
+    const char* str_value = nullptr;
+    uint8_t tmp_buf[128] = {0};
+    int32_t wbytes = 0;
+    
+    if (slot_desc->is_nullable()) {
+        auto* nullable_column =
+            reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+        nullable_column->get_null_map_data().push_back(0);
+        column_ptr = &nullable_column->get_nested_column();
+    }
+
+    switch (value->GetType()) {
+    case rapidjson::Type::kStringType:
+        str_value = value->GetString();
+        wbytes = strlen(str_value);
+        break;
+    case rapidjson::Type::kNumberType:
+        if (value->IsUint()) { 
+            wbytes = sprintf((char *)tmp_buf, "%u", value->GetUint());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsInt()) {
+            wbytes = sprintf((char *)tmp_buf, "%d", value->GetInt());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsUint64()) {
+            wbytes = sprintf((char *)tmp_buf, "%lu", value->GetUint64());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsInt64()) {
+            wbytes = sprintf((char *)tmp_buf, "%ld", value->GetInt64());
+            str_value = (char *)tmp_buf;
+        } else {
+            wbytes = sprintf((char *)tmp_buf, "%f", value->GetDouble());
+            str_value = (char *)tmp_buf;
+        }
+        break;
+    case rapidjson::Type::kFalseType:
+        wbytes = 1;
+        str_value = (char *)"0";
+        break;
+    case rapidjson::Type::kTrueType:
+        wbytes = 1;
+        str_value = (char *)"1";
+        break;
+    case rapidjson::Type::kNullType:
+        if (slot_desc->is_nullable()) {
+            auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+            nullable_column->insert_data(nullptr, 0);
+        } else {
+            RETURN_IF_ERROR(_state->append_error_msg_to_file([&]() -> std::string { return JsonReader::_print_json_value(*value); },
+                    [&]() -> std::string {
+                    fmt::memory_buffer error_msg;
+                    fmt::format_to(error_msg, "Json value is null, but the column `{}` is not nullable.", slot_desc->col_name());
+                    return fmt::to_string(error_msg);
+                    }, _scanner_eof));
+            _counter->num_rows_filtered++;
+            *valid = false;
+            return Status::OK();
+        }
+        break;
+    default:
+        // for other type like array or object. we convert it to string to save
+        std::string json_str = JsonReader::_print_json_value(*value);
+        wbytes = json_str.size();
+        str_value = json_str.c_str();
+        break;
+    }
+    
+    RETURN_IF_ERROR(_insert_to_column(column_ptr, slot_desc, str_value, wbytes));
+
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_insert_to_column(vectorized::IColumn* column_ptr, SlotDescriptor* slot_desc,
+                                    const char* value_ptr, int32_t& wbytes) {
+    switch (slot_desc->type().type) {
+    case TYPE_BOOLEAN: {
+        assert_cast<ColumnVector<UInt8>*>(column_ptr)->insert_data(value_ptr, 0);
+        break;
+    }
+    case TYPE_TINYINT: {
+        assert_cast<ColumnVector<Int8>*>(column_ptr)->insert_data(value_ptr, 0);
+        break;
+    }
+    case TYPE_SMALLINT: {
+        assert_cast<ColumnVector<Int16>*>(column_ptr)->insert_data(value_ptr, 0);
+        break;
+    }
+    case TYPE_INT: {
+        assert_cast<ColumnVector<Int32>*>(column_ptr)->insert_data(value_ptr, 0);
+        break;
+    }
+    case TYPE_BIGINT: {
+        assert_cast<ColumnVector<Int64>*>(column_ptr)->insert_data(value_ptr, 0);
+        break;
+    }
+    case TYPE_LARGEINT: {
+        assert_cast<ColumnVector<Int128>*>(column_ptr)->insert_data(value_ptr, 0);
+        break;
+    }
+    case TYPE_FLOAT: {
+        assert_cast<ColumnVector<Float32>*>(column_ptr)->insert_data(value_ptr, 0);
+        break;
+    }
+    case TYPE_DOUBLE: {
+        assert_cast<ColumnVector<Float64>*>(column_ptr)->insert_data(value_ptr, 0);
+        break;
+    }
+    case TYPE_CHAR: {
+        assert_cast<ColumnString*>(column_ptr)->insert_data(value_ptr, wbytes);
+        break;
+    }
+    case TYPE_VARCHAR:
+    case TYPE_STRING: {
+        assert_cast<ColumnString*>(column_ptr)->insert_data(value_ptr, wbytes);
+        break;
+    }
+    case TYPE_OBJECT: {
+        Slice slice(value_ptr, wbytes);
+        // insert_default()
+        auto* target_column = assert_cast<ColumnBitmap*>(column_ptr);
+
+        target_column->insert_default();
+        BitmapValue* pvalue = nullptr;
+        int pos = target_column->size() - 1;
+        pvalue = &target_column->get_element(pos);
+
+        if (slice.size != 0) {
+            BitmapValue value;
+            value.deserialize(slice.data);
+            *pvalue = std::move(value);
+        } else {
+            *pvalue = std::move(*reinterpret_cast<BitmapValue*>(slice.data));
+        }
+        break;
+    }
+    case TYPE_HLL: {
+        Slice slice(value_ptr, wbytes);
+        auto* target_column = assert_cast<ColumnHLL*>(column_ptr);
+
+        target_column->insert_default();
+        HyperLogLog* pvalue = nullptr;
+        int pos = target_column->size() - 1;
+        pvalue = &target_column->get_element(pos);
+        if (slice.size != 0) {
+            HyperLogLog value;
+            value.deserialize(slice);
+            *pvalue = std::move(value);
+        } else {
+            *pvalue = std::move(*reinterpret_cast<HyperLogLog*>(slice.data));
+        }
+        break;
+    }
+    case TYPE_DECIMALV2: {
+        assert_cast<ColumnDecimal<Decimal128>*>(column_ptr)
+                ->insert_data(value_ptr, 0);
+        break;
+    }
+    case TYPE_DATETIME: {
+        Slice slice(value_ptr, wbytes);
+        DateTimeValue value = *reinterpret_cast<DateTimeValue*>(slice.data);
+        VecDateTimeValue date;
+        date.convert_dt_to_vec_dt(&value);
+        assert_cast<ColumnVector<Int64>*>(column_ptr)
+                ->insert_data(reinterpret_cast<char*>(&date), 0);
+        break;
+    }
+    case TYPE_DATE: {
+        Slice slice(value_ptr, wbytes);
+        DateTimeValue value = *reinterpret_cast<DateTimeValue*>(slice.data);
+        VecDateTimeValue date;
+        date.convert_dt_to_vec_dt(&value);
+        assert_cast<ColumnVector<Int64>*>(column_ptr)
+                ->insert_data(reinterpret_cast<char*>(&date), 0);
+        break;
+    }
+    default: {
+        DCHECK(false) << "bad slot type: " << slot_desc->type();
+        break;
+    }
+    }
+    return Status::OK();
+}
+
+Status VJsonReader::_vhandle_flat_array_complex_json(std::vector<MutableColumnPtr>& columns,

Review Comment:
   80% the code of `_vhandle_flat_array_complex_json` of `_vhandle_nested_complex_json` is same, you shoud reuse it.



-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r866613578


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,601 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+
+            RETURN_IF_ERROR(filter_block_and_execute_exprs(&output_block, temp_block.get(), slot_num));
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+            
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                auto old_rows = output_block.rows();
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+                _counter->num_rows_unselected += old_rows - output_block.rows();
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    
+    // init file reader
+    RETURN_IF_ERROR(JsonScanner::open_file_reader());
+
+    // init line reader
+    if (_read_json_by_line) {
+        RETURN_IF_ERROR(JsonScanner::open_line_reader());
+    }
+
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    if (range.__isset.jsonpaths) {
+        jsonpath = range.jsonpaths;
+    }
+    if (range.__isset.json_root) {
+        json_root = range.json_root;
+    }
+    if (range.__isset.strip_outer_array) {
+        strip_outer_array = range.strip_outer_array;
+    }
+    if (range.__isset.num_as_string) {
+        num_as_string = range.num_as_string;
+    }
+    if (range.__isset.fuzzy_parse) {
+        fuzzy_parse = range.fuzzy_parse;
+    }
+    
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                               fuzzy_parse, &_scanner_eof, nullptr, _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                                        fuzzy_parse, &_scanner_eof, _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                        bool strip_outer_array, bool num_as_string,bool fuzzy_parse,
+                        bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+               : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                            scanner_eof, file_reader, line_reader),
+                _vhandle_json_callback(nullptr) {
+}
+
+VJsonReader::~VJsonReader() {
+
+}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = JsonReader::_generate_json_paths(jsonpath, &_parsed_jsonpaths);
+        RETURN_IF_ERROR(st);
+    }
+    if (!json_root.empty()) {
+        JsonFunctions::parse_json_paths(json_root, &_parsed_json_root);
+    }
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+    
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns, 
+                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                    bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns, 
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            Status st = _parse_json(is_empty_row, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st);
+            if (*is_empty_row == true && st == Status::OK()) {
+                return Status::OK();
+            }   
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    std::string err_msg("Empty json line");
+                    RETURN_IF_ERROR(_append_error_msg(*_json_doc, err_msg, nullptr));
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) { // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue, std::vector<MutableColumnPtr>& columns,
+                                    const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        std::string err_msg("Expect json object value");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);
+                nullcount++;
+            } else {
+                fmt::memory_buffer error_msg;
+                fmt::format_to(error_msg, "The column `{}` is not nullable, but it's not found in jsondata.", slot_desc->col_name());
+                std::string err_msg = fmt::to_string(error_msg);
+                RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+                break;
+            }
+        }
+    }
+
+    if (nullcount == slot_descs.size()) {
+        std::string err_msg("All fields is null, this is a invalid row.");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+        return Status::OK();
+    }
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_write_data_to_column(rapidjson::Value::ConstValueIterator value, SlotDescriptor* slot_desc,
+                                        vectorized::IColumn* column_ptr, bool* valid) {
+    const char* str_value = nullptr;
+    uint8_t tmp_buf[128] = {0};

Review Comment:
   Is 128 too big here?



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867463402


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,545 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() {
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status;
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::unique_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);

Review Comment:
   I think it can be removed.



-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867671833


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,517 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {}
+
+Status VJsonScanner::get_next(vectorized::Block* output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+    }
+
+    SCOPED_TIMER(_materialize_timer);
+    RETURN_IF_ERROR(BaseScanner::fill_dest_block(output_block, columns));
+
+    *eof = _scanner_eof;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    RETURN_IF_ERROR(JsonScanner::open_based_reader());
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    RETURN_IF_ERROR(JsonScanner::get_range_params(jsonpath, json_root, strip_outer_array,
+                                                  num_as_string, fuzzy_parse));
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof, nullptr,
+                                                _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof,
+                                                _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                         bool strip_outer_array, bool num_as_string, bool fuzzy_parse,
+                         bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+        : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                     scanner_eof, file_reader, line_reader),
+          _vhandle_json_callback(nullptr) {}
+
+VJsonReader::~VJsonReader() {}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // generate _parsed_jsonpaths and _parsed_json_root
+    RETURN_IF_ERROR(JsonReader::_parse_jsonpath_and_json_root(jsonpath, json_root));
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns,
+                                     const std::vector<SlotDescriptor*>& slot_descs,
+                                     bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns,
+                                         const std::vector<SlotDescriptor*>& slot_descs,
+                                         bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            Status st = _parse_json(is_empty_row, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st);
+            if (*is_empty_row == true && st == Status::OK()) {
+                return Status::OK();
+            }

Review Comment:
   `st == Status::OK()` can be removed here



-- 
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] [incubator-doris] HappenLee commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r866578414


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,601 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+
+            RETURN_IF_ERROR(filter_block_and_execute_exprs(&output_block, temp_block.get(), slot_num));
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+            
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                auto old_rows = output_block.rows();
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+                _counter->num_rows_unselected += old_rows - output_block.rows();
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    
+    // init file reader
+    RETURN_IF_ERROR(JsonScanner::open_file_reader());
+
+    // init line reader
+    if (_read_json_by_line) {
+        RETURN_IF_ERROR(JsonScanner::open_line_reader());
+    }
+
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    if (range.__isset.jsonpaths) {
+        jsonpath = range.jsonpaths;
+    }
+    if (range.__isset.json_root) {
+        json_root = range.json_root;
+    }
+    if (range.__isset.strip_outer_array) {
+        strip_outer_array = range.strip_outer_array;
+    }
+    if (range.__isset.num_as_string) {
+        num_as_string = range.num_as_string;
+    }
+    if (range.__isset.fuzzy_parse) {
+        fuzzy_parse = range.fuzzy_parse;
+    }
+    
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                               fuzzy_parse, &_scanner_eof, nullptr, _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                                        fuzzy_parse, &_scanner_eof, _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                        bool strip_outer_array, bool num_as_string,bool fuzzy_parse,
+                        bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+               : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                            scanner_eof, file_reader, line_reader),
+                _vhandle_json_callback(nullptr) {
+}
+
+VJsonReader::~VJsonReader() {
+
+}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = JsonReader::_generate_json_paths(jsonpath, &_parsed_jsonpaths);
+        RETURN_IF_ERROR(st);
+    }
+    if (!json_root.empty()) {
+        JsonFunctions::parse_json_paths(json_root, &_parsed_json_root);
+    }
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+    
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns, 
+                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                    bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns, 
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            Status st = _parse_json(is_empty_row, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st);
+            if (*is_empty_row == true && st == Status::OK()) {
+                return Status::OK();
+            }   
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    std::string err_msg("Empty json line");
+                    RETURN_IF_ERROR(_append_error_msg(*_json_doc, err_msg, nullptr));
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) { // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue, std::vector<MutableColumnPtr>& columns,
+                                    const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        std::string err_msg("Expect json object value");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);
+                nullcount++;
+            } else {
+                fmt::memory_buffer error_msg;
+                fmt::format_to(error_msg, "The column `{}` is not nullable, but it's not found in jsondata.", slot_desc->col_name());
+                std::string err_msg = fmt::to_string(error_msg);
+                RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+                break;
+            }
+        }
+    }
+
+    if (nullcount == slot_descs.size()) {
+        std::string err_msg("All fields is null, this is a invalid row.");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+        return Status::OK();
+    }
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_write_data_to_column(rapidjson::Value::ConstValueIterator value, SlotDescriptor* slot_desc,
+                                        vectorized::IColumn* column_ptr, bool* valid) {
+    const char* str_value = nullptr;
+    uint8_t tmp_buf[128] = {0};
+    int32_t wbytes = 0;
+    
+    if (slot_desc->is_nullable()) {
+        auto* nullable_column =
+            reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+        nullable_column->get_null_map_data().push_back(0);
+        column_ptr = &nullable_column->get_nested_column();
+    }
+
+    switch (value->GetType()) {
+    case rapidjson::Type::kStringType:
+        str_value = value->GetString();
+        wbytes = strlen(str_value);
+        break;
+    case rapidjson::Type::kNumberType:
+        if (value->IsUint()) { 
+            wbytes = sprintf((char *)tmp_buf, "%u", value->GetUint());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsInt()) {
+            wbytes = sprintf((char *)tmp_buf, "%d", value->GetInt());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsUint64()) {
+            wbytes = sprintf((char *)tmp_buf, "%lu", value->GetUint64());
+            str_value = (char *)tmp_buf;
+        } else if (value->IsInt64()) {
+            wbytes = sprintf((char *)tmp_buf, "%ld", value->GetInt64());
+            str_value = (char *)tmp_buf;
+        } else {
+            wbytes = sprintf((char *)tmp_buf, "%f", value->GetDouble());
+            str_value = (char *)tmp_buf;
+        }
+        break;
+    case rapidjson::Type::kFalseType:
+        wbytes = 1;
+        str_value = (char *)"0";
+        break;
+    case rapidjson::Type::kTrueType:
+        wbytes = 1;
+        str_value = (char *)"1";
+        break;
+    case rapidjson::Type::kNullType:
+        if (slot_desc->is_nullable()) {
+            auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+            nullable_column->insert_data(nullptr, 0);
+        } else {
+            fmt::memory_buffer error_msg;
+            fmt::format_to(error_msg, "Json value is null, but the column `{}` is not nullable.", slot_desc->col_name());
+            std::string err_msg = fmt::to_string(error_msg);
+            RETURN_IF_ERROR(_append_error_msg(*value, err_msg, valid));
+            return Status::OK();
+        }
+        break;
+    default:
+        // for other type like array or object. we convert it to string to save
+        std::string json_str = JsonReader::_print_json_value(*value);
+        wbytes = json_str.size();
+        str_value = json_str.c_str();
+        break;
+    }
+    
+    RETURN_IF_ERROR(_insert_to_column(column_ptr, slot_desc, str_value, wbytes));
+
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_insert_to_column(vectorized::IColumn* column_ptr, SlotDescriptor* slot_desc,
+                                    const char* value_ptr, int32_t& wbytes) {
+    switch (slot_desc->type().type) {
+        case TYPE_VARCHAR:{
+            assert_cast<ColumnString*>(column_ptr)->insert_data(value_ptr, wbytes);
+            break;
+        }
+        default: {
+            DCHECK(false) << "bad slot type: " << slot_desc->type();
+            break;
+        }
+    }
+    return Status::OK();
+}
+
+Status VJsonReader::_vhandle_flat_array_complex_json(std::vector<MutableColumnPtr>& columns,
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        if (_next_line >= _total_lines) {
+            Status st = _parse_json(is_empty_row, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st);
+            if (*is_empty_row == true ) {
+                if (st == Status::OK()) {
+                    return Status::OK();
+                }
+                if (_total_lines == 0) {
+                    continue;
+                }
+            }
+        }
+        rapidjson::Value& objectValue = (*_json_doc)[_next_line++];
+        bool valid = true;
+        RETURN_IF_ERROR(_write_columns_by_jsonpath(objectValue, slot_descs, columns, &valid));
+        if (!valid) {
+            continue; // process next line
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+Status VJsonReader::_vhandle_nested_complex_json(std::vector<MutableColumnPtr>& columns,
+                                                const std::vector<SlotDescriptor*>& slot_descs,
+                                                bool* is_empty_row, bool* eof) {
+    while (true) {
+        Status st = _parse_json(is_empty_row, eof);
+        if (st.is_data_quality_error()) {
+            continue; // continue to read next
+        }
+        RETURN_IF_ERROR(st);
+        if (*is_empty_row == true && st == Status::OK()) {
+            return Status::OK();
+        }
+        *is_empty_row = false;
+        break; // read a valid row
+    }
+    bool valid = true;
+    RETURN_IF_ERROR(_write_columns_by_jsonpath(*_json_doc, slot_descs, columns, &valid));
+    if (!valid) {
+        // there is only one line in this case, so if it return false, just set is_empty_row true
+        // so that the caller will continue reading next line.
+        *is_empty_row = true;
+    }
+    return Status::OK();
+}
+
+Status VJsonReader::_parse_json(bool* is_empty_row, bool* eof) {
+    size_t size = 0;
+    Status st = JsonReader::_parse_json_doc(&size, eof);
+    // terminate if encounter other errors
+    RETURN_IF_ERROR(st);
+    
+    // read all data, then return
+    if (size == 0 || *eof) { 
+        *is_empty_row = true;
+        return Status::OK();
+    }
+    
+    if (!_parsed_jsonpaths.empty() && _strip_outer_array) {
+        _total_lines = _json_doc->Size();
+        _next_line = 0;
+
+        if (_total_lines == 0) {
+            // meet an empty json array.
+            *is_empty_row = true;
+        }
+    }
+    return st;
+}
+
+Status VJsonReader::_write_columns_by_jsonpath(rapidjson::Value& objectValue,
+                                             const std::vector<SlotDescriptor*>& slot_descs,
+                                             std::vector<MutableColumnPtr>& columns,
+                                             bool* valid) {
+    int nullcount = 0;
+    int ctx_idx = 0;
+    size_t column_num = slot_descs.size();
+    for (size_t i = 0; i < column_num; i++) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value* json_values = nullptr;
+        bool wrap_explicitly = false;
+        if (LIKELY(i < _parsed_jsonpaths.size())) {
+            json_values = JsonFunctions::get_json_array_from_parsed_json(
+                    _parsed_jsonpaths[i], &objectValue, _origin_json_doc.GetAllocator(), &wrap_explicitly);
+        }
+
+        if (json_values == nullptr) {
+            // not match in jsondata.
+            if (slot_descs[i]->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);
+                nullcount++;
+            } else {
+                fmt::memory_buffer error_msg;
+                fmt::format_to(error_msg, "The column `{}` is not nullable, but it's not found in jsondata.", slot_descs[i]->col_name());
+                std::string err_msg = fmt::to_string(error_msg);
+                RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+                break;
+            }
+        } else {
+            CHECK(json_values->IsArray());
+            if (json_values->Size() == 1 && wrap_explicitly) {
+                // NOTICE1: JsonFunctions::get_json_array_from_parsed_json() will wrap the single json object with an array.
+                // so here we unwrap the array to get the real element.
+                // if json_values' size > 1, it means we just match an array, not a wrapped one, so no need to unwrap.
+                json_values = &((*json_values)[0]);
+            }
+            RETURN_IF_ERROR(_write_data_to_column(json_values, slot_descs[i], column_ptr, valid));
+            if (!(*valid)) {
+                break;
+            }
+        }
+    }
+
+    if (nullcount == column_num) {
+        std::string err_msg("All fields is null or not matched, this is a invalid row.");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+    }
+    return Status::OK();
+}
+
+Status VJsonReader::_append_error_msg(const rapidjson::Value& objectValue, std::string& error_msg, bool* valid) {   

Review Comment:
   use fmt::to_string and template to simplify code
   
   



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867719798


##########
be/src/exec/base_scanner.h:
##########
@@ -55,7 +57,7 @@ class BaseScanner {
                 const std::vector<TExpr>& pre_filter_texprs, ScannerCounter* counter);
     virtual ~BaseScanner() {
         Expr::close(_dest_expr_ctx, _state);
-        if (_state->enable_vectorized_exec()) {
+        if (_state != nullptr && _state->enable_vectorized_exec()) {

Review Comment:
   yes, it should be removed.



-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867345067


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,545 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() {
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status;
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::unique_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        auto n_columns = 0;
+        for (const auto slot_desc : _src_slot_descs) {
+            temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                     slot_desc->get_data_type_ptr(),
+                                                     slot_desc->col_name()));
+        }
+
+        RETURN_IF_ERROR(BaseScanner::filter_block(temp_block.get(), slot_num));
+
+        if (_dest_vexpr_ctx.empty()) {
+            output_block = *(temp_block.get());
+        } else {
+            RETURN_IF_ERROR(BaseScanner::execute_exprs(&output_block, temp_block.get()));
+        }
+    }
+
+    *eof = _scanner_eof;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    RETURN_IF_ERROR(JsonScanner::open_based_reader());
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    RETURN_IF_ERROR(JsonScanner::get_range_params(jsonpath, json_root, strip_outer_array,
+                                                  num_as_string, fuzzy_parse));
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof, nullptr,
+                                                _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof,
+                                                _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                         bool strip_outer_array, bool num_as_string, bool fuzzy_parse,
+                         bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+        : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                     scanner_eof, file_reader, line_reader),
+          _vhandle_json_callback(nullptr) {}
+
+VJsonReader::~VJsonReader() {}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // generate _parsed_jsonpaths and _parsed_json_root
+    RETURN_IF_ERROR(JsonReader::_parse_jsonpath_and_json_root(jsonpath, json_root));
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns,
+                                     const std::vector<SlotDescriptor*>& slot_descs,
+                                     bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns,
+                                         const std::vector<SlotDescriptor*>& slot_descs,
+                                         bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            Status st = _parse_json(is_empty_row, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st);
+            if (*is_empty_row == true && st == Status::OK()) {
+                return Status::OK();
+            }
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    RETURN_IF_ERROR(_append_error_msg(*_json_doc, "Empty json line", "", nullptr));
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) {                                   // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue,
+                                      std::vector<MutableColumnPtr>& columns,
+                                      const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        RETURN_IF_ERROR(_append_error_msg(objectValue, "Expect json object value", "", valid));
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);

Review Comment:
   better use `insert_default`



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r866776234


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,601 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+
+            RETURN_IF_ERROR(filter_block_and_execute_exprs(&output_block, temp_block.get(), slot_num));
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+            
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                auto old_rows = output_block.rows();
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+                _counter->num_rows_unselected += old_rows - output_block.rows();
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    
+    // init file reader
+    RETURN_IF_ERROR(JsonScanner::open_file_reader());
+
+    // init line reader
+    if (_read_json_by_line) {
+        RETURN_IF_ERROR(JsonScanner::open_line_reader());
+    }
+
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    if (range.__isset.jsonpaths) {
+        jsonpath = range.jsonpaths;
+    }
+    if (range.__isset.json_root) {
+        json_root = range.json_root;
+    }
+    if (range.__isset.strip_outer_array) {
+        strip_outer_array = range.strip_outer_array;
+    }
+    if (range.__isset.num_as_string) {
+        num_as_string = range.num_as_string;
+    }
+    if (range.__isset.fuzzy_parse) {
+        fuzzy_parse = range.fuzzy_parse;
+    }
+    
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                               fuzzy_parse, &_scanner_eof, nullptr, _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                                        fuzzy_parse, &_scanner_eof, _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                        bool strip_outer_array, bool num_as_string,bool fuzzy_parse,
+                        bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+               : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                            scanner_eof, file_reader, line_reader),
+                _vhandle_json_callback(nullptr) {
+}
+
+VJsonReader::~VJsonReader() {
+
+}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = JsonReader::_generate_json_paths(jsonpath, &_parsed_jsonpaths);
+        RETURN_IF_ERROR(st);
+    }
+    if (!json_root.empty()) {
+        JsonFunctions::parse_json_paths(json_root, &_parsed_json_root);
+    }
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+    
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns, 
+                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                    bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns, 
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            Status st = _parse_json(is_empty_row, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st);
+            if (*is_empty_row == true && st == Status::OK()) {
+                return Status::OK();
+            }   
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    std::string err_msg("Empty json line");
+                    RETURN_IF_ERROR(_append_error_msg(*_json_doc, err_msg, nullptr));
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) { // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue, std::vector<MutableColumnPtr>& columns,
+                                    const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        std::string err_msg("Expect json object value");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);
+                nullcount++;
+            } else {
+                fmt::memory_buffer error_msg;
+                fmt::format_to(error_msg, "The column `{}` is not nullable, but it's not found in jsondata.", slot_desc->col_name());
+                std::string err_msg = fmt::to_string(error_msg);
+                RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+                break;
+            }
+        }
+    }
+
+    if (nullcount == slot_descs.size()) {
+        std::string err_msg("All fields is null, this is a invalid row.");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+        return Status::OK();
+    }
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_write_data_to_column(rapidjson::Value::ConstValueIterator value, SlotDescriptor* slot_desc,
+                                        vectorized::IColumn* column_ptr, bool* valid) {
+    const char* str_value = nullptr;
+    uint8_t tmp_buf[128] = {0};

Review Comment:
   yes,  uint8_t is same as char, I will re-check.



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867290541


##########
be/src/exec/json_scanner.h:
##########
@@ -68,13 +68,13 @@ class JsonScanner : public BaseScanner {
     // Close this scanner
     void close() override;
 
-private:
+protected:
     Status open_file_reader();
     Status open_line_reader();
     Status open_json_reader();
     Status open_next_reader();
 
-private:
+protected:

Review Comment:
   > Maybe we can delete some redundant protected
   
   In order to distinguish the function and variables , I prefer to use 2 'protected' key words.



-- 
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] [incubator-doris] HappenLee commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
HappenLee commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867329627


##########
be/src/exec/base_scanner.cpp:
##########
@@ -260,6 +279,33 @@ Status BaseScanner::_fill_dest_tuple(Tuple* dest_tuple, MemPool* mem_pool) {
     return Status::OK();
 }
 
+Status BaseScanner::filter_block(vectorized::Block* temp_block, size_t slot_num) {
+    // filter src tuple by preceding filter first
+    auto old_rows = temp_block->rows();

Review Comment:
   rethink the logic?



-- 
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] [incubator-doris] xiepengcheng01 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
xiepengcheng01 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867340320


##########
be/src/exec/base_scanner.cpp:
##########
@@ -260,6 +279,33 @@ Status BaseScanner::_fill_dest_tuple(Tuple* dest_tuple, MemPool* mem_pool) {
     return Status::OK();
 }
 
+Status BaseScanner::filter_block(vectorized::Block* temp_block, size_t slot_num) {
+    // filter src tuple by preceding filter first

Review Comment:
   should modify this note



##########
be/src/exec/base_scanner.cpp:
##########
@@ -284,6 +330,12 @@ void BaseScanner::close() {
     if (!_pre_filter_ctxs.empty()) {
         Expr::close(_pre_filter_ctxs, _state);
     }
+
+    if (_state != nullptr && _state->enable_vectorized_exec()) {

Review Comment:
   no need `_state != nullptr`?



##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,545 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() {
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status;
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::unique_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        auto n_columns = 0;
+        for (const auto slot_desc : _src_slot_descs) {
+            temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                     slot_desc->get_data_type_ptr(),
+                                                     slot_desc->col_name()));
+        }
+
+        RETURN_IF_ERROR(BaseScanner::filter_block(temp_block.get(), slot_num));

Review Comment:
   should update _counter->num_rows_unselected ?



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867341749


##########
be/src/exec/base_scanner.cpp:
##########
@@ -260,6 +279,33 @@ Status BaseScanner::_fill_dest_tuple(Tuple* dest_tuple, MemPool* mem_pool) {
     return Status::OK();
 }
 
+Status BaseScanner::filter_block(vectorized::Block* temp_block, size_t slot_num) {
+    // filter src tuple by preceding filter first

Review Comment:
   yes, should be more accurate.



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867463193


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,545 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() {
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    Status status;
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::unique_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        auto n_columns = 0;
+        for (const auto slot_desc : _src_slot_descs) {
+            temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                     slot_desc->get_data_type_ptr(),
+                                                     slot_desc->col_name()));
+        }
+
+        RETURN_IF_ERROR(BaseScanner::filter_block(temp_block.get(), slot_num));
+
+        if (_dest_vexpr_ctx.empty()) {
+            output_block = *(temp_block.get());
+        } else {
+            RETURN_IF_ERROR(BaseScanner::execute_exprs(&output_block, temp_block.get()));

Review Comment:
   ok,I will re-check it.



-- 
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] [incubator-doris] xy720 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
xy720 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r862481511


##########
be/src/exec/base_scanner.h:
##########
@@ -65,6 +72,10 @@ class BaseScanner {
     virtual Status get_next(std::vector<vectorized::MutableColumnPtr>& columns, bool* eof) {
         return Status::NotSupported("Not Implemented get block");
     }
+    
+    virtual Status get_next(vectorized::Block& output_block, bool* eof) {

Review Comment:
   I only see it be implemented in vJsonScanner. May be you also need to implement it in vBrokerScanner, too.



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r865515332


##########
be/src/exec/base_scanner.h:
##########
@@ -65,6 +72,10 @@ class BaseScanner {
     virtual Status get_next(std::vector<vectorized::MutableColumnPtr>& columns, bool* eof) {
         return Status::NotSupported("Not Implemented get block");
     }
+    
+    virtual Status get_next(vectorized::Block& output_block, bool* eof) {

Review Comment:
   yes, it has been implemented in this pr :
   https://github.com/apache/incubator-doris/pull/9314/files



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r866619387


##########
be/src/exec/json_scanner.h:
##########
@@ -68,13 +68,13 @@ class JsonScanner : public BaseScanner {
     // Close this scanner
     void close() override;
 
-private:
+protected:
     Status open_file_reader();
     Status open_line_reader();
     Status open_json_reader();
     Status open_next_reader();
 
-private:
+protected:

Review Comment:
   I will re-check it.



-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r866611646


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,601 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {
+}
+
+VJsonScanner::~VJsonScanner() {
+    close();
+}
+
+Status VJsonScanner::open() {
+    RETURN_IF_ERROR(BaseScanner::open());
+    return Status::OK();
+}
+
+void VJsonScanner::close() { 
+    BaseScanner::close();
+}
+
+Status VJsonScanner::get_next(vectorized::Block& output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::shared_ptr<vectorized::Block> temp_block(new vectorized::Block());
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+        
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs,  
+                                                        &is_empty_row, &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+        SCOPED_TIMER(_materialize_timer);
+    }
+
+    if (columns[0]->size() > 0) {
+        if (!_dest_vexpr_ctx.empty()) {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                temp_block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+
+            RETURN_IF_ERROR(filter_block_and_execute_exprs(&output_block, temp_block.get(), slot_num));
+        } else {
+            auto n_columns = 0;
+            for (const auto slot_desc : _src_slot_descs) {
+                output_block.insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
+                                                        slot_desc->get_data_type_ptr(),
+                                                        slot_desc->col_name()));
+            }
+            
+            // filter src tuple by preceding filter first
+            if (!_vpre_filter_ctxs.empty()) {
+                auto old_rows = output_block.rows();
+                for (auto _vpre_filter_ctx : _vpre_filter_ctxs) {
+                    RETURN_IF_ERROR(VExprContext::filter_block(_vpre_filter_ctx, &output_block, slot_num));
+                }
+                _counter->num_rows_unselected += old_rows - output_block.rows();
+            }
+        }
+    }
+
+    if (_scanner_eof) {
+        *eof = true;
+    } else {
+        *eof = false;
+    }
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    
+    // init file reader
+    RETURN_IF_ERROR(JsonScanner::open_file_reader());
+
+    // init line reader
+    if (_read_json_by_line) {
+        RETURN_IF_ERROR(JsonScanner::open_line_reader());
+    }
+
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    const TBrokerRangeDesc& range = _ranges[_next_range];
+
+    if (range.__isset.jsonpaths) {
+        jsonpath = range.jsonpaths;
+    }
+    if (range.__isset.json_root) {
+        json_root = range.json_root;
+    }
+    if (range.__isset.strip_outer_array) {
+        strip_outer_array = range.strip_outer_array;
+    }
+    if (range.__isset.num_as_string) {
+        num_as_string = range.num_as_string;
+    }
+    if (range.__isset.fuzzy_parse) {
+        fuzzy_parse = range.fuzzy_parse;
+    }
+    
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                               fuzzy_parse, &_scanner_eof, nullptr, _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array, num_as_string,
+                                        fuzzy_parse, &_scanner_eof, _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                        bool strip_outer_array, bool num_as_string,bool fuzzy_parse,
+                        bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+               : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                            scanner_eof, file_reader, line_reader),
+                _vhandle_json_callback(nullptr) {
+}
+
+VJsonReader::~VJsonReader() {
+
+}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // parse jsonpath
+    if (!jsonpath.empty()) {
+        Status st = JsonReader::_generate_json_paths(jsonpath, &_parsed_jsonpaths);
+        RETURN_IF_ERROR(st);
+    }
+    if (!json_root.empty()) {
+        JsonFunctions::parse_json_paths(json_root, &_parsed_json_root);
+    }
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+    
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns, 
+                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                    bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns, 
+                                                    const std::vector<SlotDescriptor*>& slot_descs,
+                                                    bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            Status st = _parse_json(is_empty_row, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st);
+            if (*is_empty_row == true && st == Status::OK()) {
+                return Status::OK();
+            }   
+            _name_map.clear();
+            rapidjson::Value* objectValue = nullptr;
+            if (_json_doc->IsArray()) {
+                _total_lines = _json_doc->Size();
+                if (_total_lines == 0) {
+                    // may be passing an empty json, such as "[]"
+                    std::string err_msg("Empty json line");
+                    RETURN_IF_ERROR(_append_error_msg(*_json_doc, err_msg, nullptr));
+                    if (*_scanner_eof) {
+                        *is_empty_row = true;
+                        return Status::OK();
+                    }
+                    continue;
+                }
+                objectValue = &(*_json_doc)[0];
+            } else {
+                _total_lines = 1; // only one row
+                objectValue = _json_doc;
+            }
+            _next_line = 0;
+            if (_fuzzy_parse) {
+                for (auto v : slot_descs) {
+                    for (int i = 0; i < objectValue->MemberCount(); ++i) {
+                        auto it = objectValue->MemberBegin() + i;
+                        if (v->col_name() == it->name.GetString()) {
+                            _name_map[v->col_name()] = i;
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
+        if (_json_doc->IsArray()) { // handle case 1
+            rapidjson::Value& objectValue = (*_json_doc)[_next_line]; // json object
+            RETURN_IF_ERROR(_set_column_value(objectValue, columns, slot_descs, &valid));
+        } else { // handle case 2
+            RETURN_IF_ERROR(_set_column_value(*_json_doc, columns, slot_descs, &valid));
+        }
+        _next_line++;
+        if (!valid) {
+            if (*_scanner_eof) {
+                // When _scanner_eof is true and valid is false, it means that we have encountered
+                // unqualified data and decided to stop the scan.
+                *is_empty_row = true;
+                return Status::OK();
+            }
+            continue;
+        }
+        *is_empty_row = false;
+        break; // get a valid row, then break
+    } while (_next_line <= _total_lines);
+    return Status::OK();
+}
+
+// for simple format json
+// set valid to true and return OK if succeed.
+// set valid to false and return OK if we met an invalid row.
+// return other status if encounter other problmes.
+Status VJsonReader::_set_column_value(rapidjson::Value& objectValue, std::vector<MutableColumnPtr>& columns,
+                                    const std::vector<SlotDescriptor*>& slot_descs, bool* valid) {
+    if (!objectValue.IsObject()) {
+        // Here we expect the incoming `objectValue` to be a Json Object, such as {"key" : "value"},
+        // not other type of Json format.
+        std::string err_msg("Expect json object value");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+        return Status::OK();
+    }
+
+    int nullcount = 0;
+    int ctx_idx = 0;
+    for (auto slot_desc : slot_descs) {
+        int dest_index = ctx_idx++;
+        auto* column_ptr = columns[dest_index].get();
+        rapidjson::Value::ConstMemberIterator it = objectValue.MemberEnd();
+
+        if (_fuzzy_parse) {
+            auto idx_it = _name_map.find(slot_desc->col_name());
+            if (idx_it != _name_map.end() && idx_it->second < objectValue.MemberCount()) {
+                it = objectValue.MemberBegin() + idx_it->second;
+            }
+        } else {
+            it = objectValue.FindMember(
+                    rapidjson::Value(slot_desc->col_name().c_str(), slot_desc->col_name().size()));
+        }
+
+        if (it != objectValue.MemberEnd()) {
+            const rapidjson::Value& value = it->value;
+            RETURN_IF_ERROR(_write_data_to_column(&value, slot_desc, column_ptr, valid));
+            if (!(*valid)) {
+                return Status::OK();
+            }
+        } else { // not found
+            if (slot_desc->is_nullable()) {
+                auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(column_ptr);
+                nullable_column->insert_data(nullptr, 0);
+                nullcount++;
+            } else {
+                fmt::memory_buffer error_msg;
+                fmt::format_to(error_msg, "The column `{}` is not nullable, but it's not found in jsondata.", slot_desc->col_name());
+                std::string err_msg = fmt::to_string(error_msg);
+                RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+                break;
+            }
+        }
+    }
+
+    if (nullcount == slot_descs.size()) {
+        std::string err_msg("All fields is null, this is a invalid row.");
+        RETURN_IF_ERROR(_append_error_msg(objectValue, err_msg, valid));
+        return Status::OK();
+    }
+    *valid = true;
+    return Status::OK();
+}
+
+Status VJsonReader::_write_data_to_column(rapidjson::Value::ConstValueIterator value, SlotDescriptor* slot_desc,
+                                        vectorized::IColumn* column_ptr, bool* valid) {
+    const char* str_value = nullptr;
+    uint8_t tmp_buf[128] = {0};

Review Comment:
   Why not just use `char` directly?



-- 
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] [incubator-doris] xiepengcheng01 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
xiepengcheng01 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867678373


##########
be/src/vec/exec/vbroker_scan_node.h:
##########
@@ -31,7 +31,8 @@ namespace vectorized {
 class VBrokerScanNode final : public BrokerScanNode {
 public:
     VBrokerScanNode(ObjectPool* pool, const TPlanNode& tnode, const DescriptorTbl& descs);
-    ~VBrokerScanNode() override = default;
+
+    ~VBrokerScanNode() { close(_runtime_state); }

Review Comment:
   why add `close` here?



-- 
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] [incubator-doris] github-actions[bot] commented on pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#issuecomment-1121963848

   PR approved by at least one committer and no changes requested.


-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r868787335


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,521 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {}
+
+Status VJsonScanner::get_next(vectorized::Block* output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);

Review Comment:
   yes, it should be simplified.



-- 
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] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
BiteTheDDDDt commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867669760


##########
be/src/exec/base_scanner.h:
##########
@@ -55,7 +57,7 @@ class BaseScanner {
                 const std::vector<TExpr>& pre_filter_texprs, ScannerCounter* counter);
     virtual ~BaseScanner() {
         Expr::close(_dest_expr_ctx, _state);
-        if (_state->enable_vectorized_exec()) {
+        if (_state != nullptr && _state->enable_vectorized_exec()) {

Review Comment:
   remove this check



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867721338


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,517 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {}
+
+Status VJsonScanner::get_next(vectorized::Block* output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+    }
+
+    SCOPED_TIMER(_materialize_timer);
+    RETURN_IF_ERROR(BaseScanner::fill_dest_block(output_block, columns));
+
+    *eof = _scanner_eof;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    RETURN_IF_ERROR(JsonScanner::open_based_reader());
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    RETURN_IF_ERROR(JsonScanner::get_range_params(jsonpath, json_root, strip_outer_array,
+                                                  num_as_string, fuzzy_parse));
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof, nullptr,
+                                                _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof,
+                                                _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                         bool strip_outer_array, bool num_as_string, bool fuzzy_parse,
+                         bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+        : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                     scanner_eof, file_reader, line_reader),
+          _vhandle_json_callback(nullptr) {}
+
+VJsonReader::~VJsonReader() {}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // generate _parsed_jsonpaths and _parsed_json_root
+    RETURN_IF_ERROR(JsonReader::_parse_jsonpath_and_json_root(jsonpath, json_root));
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns,
+                                     const std::vector<SlotDescriptor*>& slot_descs,
+                                     bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns,
+                                         const std::vector<SlotDescriptor*>& slot_descs,
+                                         bool* is_empty_row, bool* eof) {
+    do {
+        bool valid = false;
+        if (_next_line >= _total_lines) { // parse json and generic document
+            Status st = _parse_json(is_empty_row, eof);
+            if (st.is_data_quality_error()) {
+                continue; // continue to read next
+            }
+            RETURN_IF_ERROR(st);
+            if (*is_empty_row == true && st == Status::OK()) {
+                return Status::OK();
+            }

Review Comment:
   > `st == Status::OK()` can be removed here
   when *is_empty_row == true, parse_json() has 2 branch. so need check  `st == Status::OK()`.
   



-- 
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] [incubator-doris] carlvinhust2012 commented on a diff in pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
carlvinhust2012 commented on code in PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#discussion_r867696715


##########
be/src/vec/exec/vjson_scanner.cpp:
##########
@@ -0,0 +1,517 @@
+// 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 "vec/exec/vjson_scanner.h"
+
+#include <algorithm>
+#include <fmt/format.h>
+
+#include "env/env.h"
+#include "exec/broker_reader.h"
+#include "exec/buffered_reader.h"
+#include "exec/local_file_reader.h"
+#include "exec/plain_text_line_reader.h"
+#include "exec/s3_reader.h"
+#include "exprs/expr.h"
+#include "exprs/json_functions.h"
+#include "gutil/strings/split.h"
+#include "runtime/exec_env.h"
+#include "runtime/runtime_state.h"
+#include "util/time.h"
+
+namespace doris::vectorized {
+
+VJsonScanner::VJsonScanner(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)
+        : JsonScanner(state, profile, params, ranges, broker_addresses, pre_filter_texprs, counter),
+          _cur_vjson_reader(nullptr) {}
+
+VJsonScanner::~VJsonScanner() {}
+
+Status VJsonScanner::get_next(vectorized::Block* output_block, bool* eof) {
+    SCOPED_TIMER(_read_timer);
+    const int batch_size = _state->batch_size();
+    size_t slot_num = _src_slot_descs.size();
+    std::vector<vectorized::MutableColumnPtr> columns(slot_num);
+    auto string_type = make_nullable(std::make_shared<DataTypeString>());
+    for (int i = 0; i < slot_num; i++) {
+        columns[i] = string_type->create_column();
+    }
+
+    // Get one line
+    while (columns[0]->size() < batch_size && !_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;
+            }
+        }
+
+        if (_read_json_by_line && _skip_next_line) {
+            size_t size = 0;
+            const uint8_t* line_ptr = nullptr;
+            RETURN_IF_ERROR(_cur_line_reader->read_line(&line_ptr, &size, &_cur_reader_eof));
+            _skip_next_line = false;
+            continue;
+        }
+
+        bool is_empty_row = false;
+        RETURN_IF_ERROR(_cur_vjson_reader->read_json_column(columns, _src_slot_descs, &is_empty_row,
+                                                            &_cur_reader_eof));
+        if (is_empty_row) {
+            // Read empty row, just continue
+            continue;
+        }
+        COUNTER_UPDATE(_rows_read_counter, 1);
+    }
+
+    SCOPED_TIMER(_materialize_timer);
+    RETURN_IF_ERROR(BaseScanner::fill_dest_block(output_block, columns));
+
+    *eof = _scanner_eof;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_next_reader() {
+    if (_next_range >= _ranges.size()) {
+        _scanner_eof = true;
+        return Status::OK();
+    }
+    RETURN_IF_ERROR(JsonScanner::open_based_reader());
+    RETURN_IF_ERROR(open_vjson_reader());
+    _next_range++;
+    return Status::OK();
+}
+
+Status VJsonScanner::open_vjson_reader() {
+    if (_cur_vjson_reader != nullptr) {
+        _cur_vjson_reader.reset();
+    }
+    std::string json_root = "";
+    std::string jsonpath = "";
+    bool strip_outer_array = false;
+    bool num_as_string = false;
+    bool fuzzy_parse = false;
+
+    RETURN_IF_ERROR(JsonScanner::get_range_params(jsonpath, json_root, strip_outer_array,
+                                                  num_as_string, fuzzy_parse));
+    if (_read_json_by_line) {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof, nullptr,
+                                                _cur_line_reader));
+    } else {
+        _cur_vjson_reader.reset(new VJsonReader(_state, _counter, _profile, strip_outer_array,
+                                                num_as_string, fuzzy_parse, &_scanner_eof,
+                                                _cur_file_reader));
+    }
+
+    RETURN_IF_ERROR(_cur_vjson_reader->init(jsonpath, json_root));
+    return Status::OK();
+}
+
+VJsonReader::VJsonReader(RuntimeState* state, ScannerCounter* counter, RuntimeProfile* profile,
+                         bool strip_outer_array, bool num_as_string, bool fuzzy_parse,
+                         bool* scanner_eof, FileReader* file_reader, LineReader* line_reader)
+        : JsonReader(state, counter, profile, strip_outer_array, num_as_string, fuzzy_parse,
+                     scanner_eof, file_reader, line_reader),
+          _vhandle_json_callback(nullptr) {}
+
+VJsonReader::~VJsonReader() {}
+
+Status VJsonReader::init(const std::string& jsonpath, const std::string& json_root) {
+    // generate _parsed_jsonpaths and _parsed_json_root
+    RETURN_IF_ERROR(JsonReader::_parse_jsonpath_and_json_root(jsonpath, json_root));
+
+    //improve performance
+    if (_parsed_jsonpaths.empty()) { // input is a simple json-string
+        _vhandle_json_callback = &VJsonReader::_vhandle_simple_json;
+    } else { // input is a complex json-string and a json-path
+        if (_strip_outer_array) {
+            _vhandle_json_callback = &VJsonReader::_vhandle_flat_array_complex_json;
+        } else {
+            _vhandle_json_callback = &VJsonReader::_vhandle_nested_complex_json;
+        }
+    }
+
+    return Status::OK();
+}
+
+Status VJsonReader::read_json_column(std::vector<MutableColumnPtr>& columns,
+                                     const std::vector<SlotDescriptor*>& slot_descs,
+                                     bool* is_empty_row, bool* eof) {
+    return (this->*_vhandle_json_callback)(columns, slot_descs, is_empty_row, eof);
+}
+
+Status VJsonReader::_vhandle_simple_json(std::vector<MutableColumnPtr>& columns,
+                                         const std::vector<SlotDescriptor*>& slot_descs,
+                                         bool* is_empty_row, bool* eof) {

Review Comment:
   yes, it read one batch_size every time.



-- 
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] [incubator-doris] github-actions[bot] commented on pull request #9311: [feature]add vectorized vjson_scanner

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #9311:
URL: https://github.com/apache/incubator-doris/pull/9311#issuecomment-1123265808

   PR approved by at least one committer and no changes requested.


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