Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

[NSE-653] Add validity checking for get_json_object in WSCG #654

Merged
merged 4 commits into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Run clang-format style check for C/C++ programs.
uses: jidicula/clang-format-action@v3.2.0
uses: jidicula/clang-format-action@v3.5.1
with:
clang-format-version: '10'
check-path: 'native-sql-engine/cpp/src'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,18 @@ arrow::Status ExpressionCodegenVisitor::Visit(const gandiva::FunctionNode& node)
real_codes_str_ = codes_str_;
real_validity_str_ = check_str_;
std::stringstream prepare_ss;
prepare_ss << "bool " << check_str_ << " = true;" << std::endl;
prepare_ss << "std::string " << codes_str_ << " = get_json_object("
auto validity = codes_str_ + "_validity";
prepare_ss << "std::string " << codes_str_ << ";" << std::endl;
prepare_ss << "bool " << validity << " = " << child_visitor_list[0]->GetPreCheck()
<< ";" << std::endl;
prepare_ss << "if (" << validity << ") {" << std::endl;
prepare_ss << codes_str_ << " = get_json_object("
<< child_visitor_list[0]->GetResult() << ", "
<< child_visitor_list[1]->GetResult() << ");\n";
<< child_visitor_list[1]->GetResult() << ", "
<< "&" << validity << ");\n";
prepare_ss << "}" << std::endl;
prepare_str_ += prepare_ss.str();
check_str_ = validity;
header_list_.push_back(R"(#include "precompile/gandiva.h")");
} else if (func_name.compare("substr") == 0) {
ss << child_visitor_list[0]->GetResult() << ".substr("
Expand Down
15 changes: 9 additions & 6 deletions native-sql-engine/cpp/src/precompile/gandiva.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ arrow::Decimal128 round(arrow::Decimal128 in, int32_t original_precision,
return arrow::Decimal128(out);
}

std::string get_json_object(const std::string& json_str, const std::string& json_path) {
std::string get_json_object(const std::string& json_str, const std::string& json_path,
bool* validity) {
std::unique_ptr<arrow::json::BlockParser> parser;
(arrow::json::BlockParser::Make(arrow::json::ParseOptions::Defaults(), &parser));
(parser->Parse(std::make_shared<arrow::Buffer>(json_str)));
Expand All @@ -239,27 +240,29 @@ std::string get_json_object(const std::string& json_str, const std::string& json
auto struct_parsed = std::dynamic_pointer_cast<arrow::StructArray>(parsed);
// json_path example: $.col_14, will extract col_14 here
if (json_path.length() < 3) {
return nullptr;
*validity = false;
return "";
}
auto col_name = json_path.substr(2);
// illegal json string.
if (struct_parsed == nullptr) {
return nullptr;
*validity = false;
return "";
}
auto dict_parsed = std::dynamic_pointer_cast<arrow::DictionaryArray>(
struct_parsed->GetFieldByName(col_name));
// no data contained for given field.
if (dict_parsed == nullptr) {
return nullptr;
*validity = false;
return "";
}

auto dict_array = dict_parsed->dictionary();
// needs to see whether there is a case that has more than one indices.
auto res_index = dict_parsed->GetValueIndex(0);
// TODO(): check null results
auto utf8_array = std::dynamic_pointer_cast<arrow::BinaryArray>(dict_array);

auto res = utf8_array->GetString(res_index);

*validity = true;
return res;
}
11 changes: 10 additions & 1 deletion native-sql-engine/cpp/src/tests/arrow_compute_test_precompile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,17 @@ TEST(TestArrowCompute, ArithmeticComparisonTest) {
}

TEST(TestArrowCompute, JsonTest) {
std::string data = get_json_object(R"({"hello": "3.5"})", "$.hello");
bool validity;
std::string data = get_json_object(R"({"hello": "3.5"})", "$.hello", &validity);
EXPECT_EQ(data, "3.5");
EXPECT_EQ(validity, true);

data = get_json_object(R"({"hello": ""})", "$.hello", &validity);
EXPECT_EQ(data, "");
EXPECT_EQ(validity, true);

data = get_json_object(R"({"hello": "3.5"})", "$.hi", &validity);
EXPECT_EQ(validity, false);
}

} // namespace codegen
Expand Down