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-unsubscr...@doris.apache.org

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


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

Reply via email to