| // 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 <gtest/gtest-message.h> |
| #include <gtest/gtest-test-part.h> |
| #include <rapidjson/document.h> |
| #include <rapidjson/encodings.h> |
| #include <rapidjson/stringbuffer.h> |
| #include <rapidjson/writer.h> |
| |
| #include <string> |
| #include <string_view> |
| |
| #include "exprs/json_functions.h" |
| #include "gtest/gtest_pred_impl.h" |
| |
| namespace doris { |
| |
| // mock |
| class JsonFunctionTest : public testing::Test { |
| public: |
| JsonFunctionTest() {} |
| }; |
| |
| TEST_F(JsonFunctionTest, json_path1) { |
| bool wrap_explicitly; |
| std::string json_raw_data( |
| "[{\"k1\":\"v1\",\"keyname\":{\"ip\":\"10.10.0.1\",\"value\":20}},{\"k1\":\"v1-1\"," |
| "\"keyname\":{\"ip\":\"10.20.10.1\",\"value\":20}}]"); |
| rapidjson::Document jsonDoc; |
| if (jsonDoc.Parse(json_raw_data.c_str()).HasParseError()) { |
| EXPECT_TRUE(false); |
| } |
| rapidjson::Value* res3; |
| res3 = JsonFunctions::get_json_array_from_parsed_json("$.[*].keyname.ip", &jsonDoc, |
| jsonDoc.GetAllocator(), &wrap_explicitly); |
| EXPECT_TRUE(res3->IsArray()); |
| |
| res3 = JsonFunctions::get_json_array_from_parsed_json("$.[*].k1", &jsonDoc, |
| jsonDoc.GetAllocator(), &wrap_explicitly); |
| EXPECT_TRUE(res3->IsArray()); |
| |
| res3 = JsonFunctions::get_json_array_from_parsed_json("$", &jsonDoc, jsonDoc.GetAllocator(), |
| &wrap_explicitly); |
| EXPECT_TRUE(res3->IsArray()); |
| rapidjson::StringBuffer buffer; |
| buffer.Clear(); |
| rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); |
| (*res3)[0].Accept(writer); |
| EXPECT_EQ(json_raw_data, std::string(buffer.GetString())); |
| } |
| |
| TEST_F(JsonFunctionTest, json_path_get_nullobject) { |
| bool wrap_explicitly; |
| std::string json_raw_data( |
| "[{\"a\":\"a1\", \"b\":\"b1\", \"c\":\"c1\"},{\"a\":\"a2\", " |
| "\"c\":\"c2\"},{\"a\":\"a3\", \"b\":\"b3\", \"c\":\"c3\"}]"); |
| rapidjson::Document jsonDoc; |
| if (jsonDoc.Parse(json_raw_data.c_str()).HasParseError()) { |
| EXPECT_TRUE(false); |
| } |
| |
| rapidjson::Value* res3 = JsonFunctions::get_json_array_from_parsed_json( |
| "$.[*].b", &jsonDoc, jsonDoc.GetAllocator(), &wrap_explicitly); |
| EXPECT_TRUE(res3->IsArray()); |
| EXPECT_EQ(res3->Size(), 3); |
| } |
| |
| TEST_F(JsonFunctionTest, json_path_test) { |
| bool wrap_explicitly; |
| { |
| std::string json_raw_data("[{\"a\":\"a1\", \"b\":\"b1\"}, {\"a\":\"a2\", \"b\":\"b2\"}]"); |
| rapidjson::Document jsonDoc; |
| if (jsonDoc.Parse(json_raw_data.c_str()).HasParseError()) { |
| EXPECT_TRUE(false); |
| } |
| |
| rapidjson::Value* res3 = JsonFunctions::get_json_array_from_parsed_json( |
| "$.[*].a", &jsonDoc, jsonDoc.GetAllocator(), &wrap_explicitly); |
| EXPECT_TRUE(res3->IsArray()); |
| EXPECT_EQ(res3->Size(), 2); |
| } |
| { |
| std::string json_raw_data( |
| "{\"a\":[\"a1\",\"a2\"], \"b\":[\"b1\",\"b2\"], \"c\":[\"c1\"], \"d\":[], " |
| "\"e\":\"e1\"}"); |
| rapidjson::Document jsonDoc; |
| if (jsonDoc.Parse(json_raw_data.c_str()).HasParseError()) { |
| EXPECT_TRUE(false); |
| } |
| |
| rapidjson::Value* res3 = JsonFunctions::get_json_array_from_parsed_json( |
| "$.a", &jsonDoc, jsonDoc.GetAllocator(), &wrap_explicitly); |
| EXPECT_TRUE(res3->IsArray()); |
| EXPECT_EQ(res3->Size(), 2); |
| |
| rapidjson::Value* res4 = JsonFunctions::get_json_array_from_parsed_json( |
| "$.c", &jsonDoc, jsonDoc.GetAllocator(), &wrap_explicitly); |
| EXPECT_TRUE(res4->IsArray()); |
| EXPECT_EQ(res4->Size(), 1); |
| EXPECT_FALSE(wrap_explicitly); |
| |
| rapidjson::Value* res5 = JsonFunctions::get_json_array_from_parsed_json( |
| "$.d", &jsonDoc, jsonDoc.GetAllocator(), &wrap_explicitly); |
| EXPECT_TRUE(res5->IsArray()); |
| EXPECT_EQ(res5->Size(), 0); |
| EXPECT_FALSE(wrap_explicitly); |
| |
| rapidjson::Value* res6 = JsonFunctions::get_json_array_from_parsed_json( |
| "$.e", &jsonDoc, jsonDoc.GetAllocator(), &wrap_explicitly); |
| EXPECT_TRUE(res6->IsArray()); |
| EXPECT_EQ(res6->Size(), 1); |
| EXPECT_TRUE(wrap_explicitly); |
| } |
| } |
| |
| TEST_F(JsonFunctionTest, extract_from_object_not_found_errors) { |
| const auto expect_not_found = [](std::string_view json, const std::string& path, |
| std::string_view expected_message) { |
| simdjson::ondemand::parser parser; |
| simdjson::padded_string padded_json(json.data(), json.size()); |
| auto document = parser.iterate(padded_json); |
| simdjson::ondemand::object object; |
| ASSERT_EQ(document.get_object().get(object), simdjson::SUCCESS); |
| |
| std::vector<JsonPath> parsed_paths; |
| JsonFunctions::parse_json_paths(path, &parsed_paths); |
| simdjson::ondemand::value value; |
| const Status status = JsonFunctions::extract_from_object(object, parsed_paths, &value); |
| |
| ASSERT_TRUE(status.is<ErrorCode::NOT_FOUND>()) << status; |
| EXPECT_NE(status.to_string().find(expected_message), std::string::npos) << status; |
| }; |
| |
| expect_not_found(R"({"k1":{"k2":"v2"}})", "$.k1.k3", "unable to find field: k3"); |
| expect_not_found(R"({"k1":{"k2":[1,2,3]}})", "$.k1.k2[5]", |
| "failed to access array field: k2, index: 5"); |
| expect_not_found(R"({"k1":{"k2":"v2"}})", "$.k1.k2.k3", "unable to find field: k3"); |
| } |
| |
| } // namespace doris |