Files
doris/be/test/runtime/jsonb_value_test.cpp
Shane 59699a4321 [feature](JSON datatype)Support JSON datatype (#10322)
Add `JSON` datatype, following features are implemented by this PR:
1. `CREATE` tables with `JSON` type columns
2. `INSERT` values containing `JSON` type value stored in `String`, which is represented as binary format(AKA `JSONB`) at BE 
3. `SELECT` JSON columns

Detail design refers [DSIP-016: Support JSON type](https://cwiki.apache.org/confluence/display/DORIS/DSIP-016%3A+Support+JSON+type)

* add JSONB data storage format type

* fix JsonLiteral resolve bug

* add DataTypeJson case in data_type_factory

* add JSON syntax check in FE

* add operators for jsonb_document, currently not support comparison between any JSON type value

* add ColumnJson and DataTypeJson

* add JsonField to store JsonValue

* add JsonValue to convert String JSON to BINARY JSON and JsonLiteral case for vliteral

* add push_json for MysqlResultWriter

* JSON column need no zone_map_index

* Revert "JSON column need no zone_map_index"

This reverts commit f71d1ce1ded9dbae44a5d58abcec338816b70d79.

* add JSON writer and reader, ignore zone-map for JSON column

* add json_to_string for DataTypeJson

* add olap_data_convertor for JSON type

* add some enum

* add OLAP_FIELD_TYPE_JSON type, FieldTypeTraits for it and corresponding cases or functions

* fix column_json offsets overflow bug, format code

* remove useless TODOs, add CmpType cases for JSON type

* add license header

* format license

* format be codes

* resolve rebase master conflicts

* fix bugs for CREATE and meta related code

* refactor JsonValue constructors, add fe JSON cases and fix some bugs, reformat codes

* modification be codes along code review advice

* fix rebase conflicts with master

* add unit test for json_value and column_json

* fix rebase error

* rename json to jsonb

* fix some data convert bugs, set Mysql type to JSON
2022-09-25 14:06:49 +08:00

60 lines
2.1 KiB
C++

// 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 "runtime/jsonb_value.h"
#include <gtest/gtest.h>
#include <string>
#include "util/cpu_info.h"
using std::string;
namespace doris {
JsonBinaryValue FromStdString(const string& str) {
char* ptr = const_cast<char*>(str.c_str());
int len = str.size();
return JsonBinaryValue(ptr, len);
}
TEST(JsonBinaryValueTest, TestValidation) {
JsonbErrType err;
JsonBinaryValue json_val;
// single value not wrapped as an arrar or object is invalid
std::vector<string> invalid_strs = {"", "1", "null", "false", "abc"};
for (size_t i = 0; i < invalid_strs.size(); i++) {
err = json_val.from_json_string(invalid_strs[i].c_str(), invalid_strs[i].size());
EXPECT_NE(err, JsonbErrType::E_NONE);
}
// valid enums
std::vector<string> valid_strs;
valid_strs.push_back("[false]");
valid_strs.push_back("[-123]");
valid_strs.push_back("[\"abc\"]");
valid_strs.push_back("[\"val1\", \"val2\"]");
valid_strs.push_back("{\"key1\": \"js6\", \"key2\": [\"val1\", \"val2\"]}");
valid_strs.push_back("[123, {\"key1\": null, \"key2\": [\"val1\", \"val2\"]}]");
for (size_t i = 0; i < valid_strs.size(); i++) {
err = json_val.from_json_string(valid_strs[i].c_str(), valid_strs[i].size());
EXPECT_EQ(err, JsonbErrType::E_NONE);
}
}
} // namespace doris