xy720 commented on code in PR #16444:
URL: https://github.com/apache/doris/pull/16444#discussion_r1099770228


##########
be/src/vec/data_types/data_type_struct.cpp:
##########
@@ -0,0 +1,480 @@
+// 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.
+// This file is copied from
+// 
https://github.com/ClickHouse/ClickHouse/blob/master/src/DataTypes/DataTypeTuple.cpp
+// and modified by Doris
+
+#include "vec/data_types/data_type_struct.h"
+
+namespace doris::vectorized {
+
+DataTypeStruct::DataTypeStruct(const DataTypes& elems_)
+        : elems(elems_), have_explicit_names(false) {
+    /// Automatically assigned names in form of '1', '2', ...
+    size_t size = elems.size();
+    names.resize(size);
+    for (size_t i = 0; i < size; ++i) {
+        names[i] = std::to_string(i + 1);
+    }
+}
+
+static Status check_tuple_names(const Strings& names) {
+    std::unordered_set<String> names_set;
+    for (const auto& name : names) {
+        if (name.empty()) {
+            return Status::InvalidArgument("Names of tuple elements cannot be 
empty");
+        }
+
+        if (!names_set.insert(name).second) {
+            return Status::InvalidArgument("Names of tuple elements must be 
unique");
+        }
+    }
+
+    return {};
+}
+
+DataTypeStruct::DataTypeStruct(const DataTypes& elems_, const Strings& names_)
+        : elems(elems_), names(names_), have_explicit_names(true) {
+    size_t size = elems.size();
+    if (names.size() != size) {
+        LOG(FATAL) << "Wrong number of names passed to constructor of 
DataTypeStruct";
+    }
+
+    Status st = check_tuple_names(names);
+    //if (!st.ok()) {
+    //}
+}
+
+std::string DataTypeStruct::do_get_name() const {
+    size_t size = elems.size();
+    std::stringstream s;
+
+    s << "Struct(";
+    for (size_t i = 0; i < size; ++i) {
+        if (i != 0) {
+            s << ", ";
+        }
+
+        // if (have_explicit_names) {
+        //     s << back_quote_if_need(names[i]) << ' ';
+        // }
+
+        s << elems[i]->get_name();
+    }
+    s << ")";
+
+    return s.str();
+}
+
+Status DataTypeStruct::from_string(ReadBuffer& rb, IColumn* column) const {
+    DCHECK(!rb.eof());
+    auto* struct_column = assert_cast<ColumnStruct*>(column);
+
+    if (*rb.position() != '{') {
+        return Status::InvalidArgument("Struct does not start with '{' 
character, found '{}'",
+                                       *rb.position());
+    }
+    if (rb.count() < 2 || *(rb.end() - 1) != '}') {
+        return Status::InvalidArgument("Struct does not end with '}' 
character, found '{}'",
+                                       *(rb.end() - 1));
+    }
+
+    // here need handle the empty struct '{}'
+    if (rb.count() == 2) {
+        return Status::OK();
+    }
+
+    ++rb.position();
+    std::vector<ReadBuffer> field_rbs;
+    field_rbs.reserve(elems.size());
+
+    // here get the value "jack" and 20 from {"name":"jack","age":20}
+    while (!rb.eof()) {
+        size_t field_len = 0;
+        auto start = rb.position();
+        while (!rb.eof() && *start != ',' && *start != '}') {
+            field_len++;
+            start++;
+        }
+        if (field_len >= rb.count()) {
+            return Status::InvalidArgument("Invalid Length");
+        }
+        ReadBuffer field_rb(rb.position(), field_len);
+
+        size_t len = 0;
+        auto start_rb = field_rb.position();
+        while (!field_rb.eof() && *start_rb != ':') {
+            len++;
+            start_rb++;
+        }
+        ReadBuffer field(field_rb.position() + len + 1, field_rb.count() - len 
- 1);
+
+        if (field.count() >= 2 && ((*field.position() == '"' && *(field.end() 
- 1) == '"') ||
+                                   (*field.position() == '\'' && *(field.end() 
- 1) == '\''))) {
+            ReadBuffer field_no_quote(field.position() + 1, field.count() - 2);
+            field_rbs.push_back(field_no_quote);
+        } else {
+            field_rbs.push_back(field);
+        }
+
+        rb.position() += field_len + 1;
+    }
+
+    for (size_t idx = 0; idx < elems.size(); idx++) {
+        elems[idx]->from_string(field_rbs[idx], 
&struct_column->get_column(idx));
+    }
+
+    return Status::OK();
+}
+
+std::string DataTypeStruct::to_string(const IColumn& column, size_t row_num) 
const {
+    auto ptr = column.convert_to_full_column_if_const();
+    auto& struct_column = assert_cast<const ColumnStruct&>(*ptr.get());
+
+    std::stringstream ss;
+    ss << "<";

Review Comment:
   done, modify to '{'



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