forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement json functions (apache#49)
Signed-off-by: Yuan Zhou <yuan.zhou@intel.com>
- Loading branch information
1 parent
557a866
commit 0dcc45c
Showing
8 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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 "gandiva/json_holder.h" | ||
|
||
#include <iostream> | ||
#include <regex> | ||
|
||
#include "gandiva/node.h" | ||
#include "gandiva/regex_util.h" | ||
|
||
namespace gandiva { | ||
|
||
Status JsonHolder::Make(const FunctionNode& node, std::shared_ptr<JsonHolder>* holder) { | ||
return Make(holder); | ||
} | ||
|
||
Status JsonHolder::Make(std::shared_ptr<JsonHolder>* holder) { | ||
*holder = std::shared_ptr<JsonHolder>(new JsonHolder()); | ||
return Status::OK(); | ||
} | ||
|
||
const uint8_t* JsonHolder::operator()(const std::string& json_str, const std::string& json_path, int32_t* out_len) { | ||
|
||
std::unique_ptr<arrow::json::BlockParser> parser; | ||
(arrow::json::BlockParser::Make(parse_options_, &parser)); | ||
|
||
(parser->Parse(std::make_shared<arrow::Buffer>(json_str))); | ||
std::shared_ptr<arrow::Array> parsed; | ||
(parser->Finish(&parsed)); | ||
auto struct_parsed = std::dynamic_pointer_cast<arrow::StructArray>(parsed); | ||
|
||
//json_path example: $.col_14, will extract col_14 here | ||
// needs to gurad failure here | ||
auto col_name = json_path.substr(2); | ||
|
||
auto dict_parsed = std::dynamic_pointer_cast<arrow::DictionaryArray>( | ||
struct_parsed->GetFieldByName(col_name)); | ||
auto dict_array = dict_parsed->dictionary(); | ||
auto uft8_array = std::dynamic_pointer_cast<arrow::BinaryArray>(dict_array); | ||
|
||
return uft8_array->GetValue(0, out_len); | ||
} | ||
|
||
} // namespace gandiva |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "arrow/json/api.h" | ||
#include "arrow/json/parser.h" | ||
#include "arrow/status.h" | ||
#include "gandiva/function_holder.h" | ||
#include "gandiva/node.h" | ||
#include "gandiva/visibility.h" | ||
|
||
namespace gandiva { | ||
|
||
/// Function Holder for SQL 'get_json_object' | ||
class GANDIVA_EXPORT JsonHolder : public FunctionHolder { | ||
public: | ||
JsonHolder() {} | ||
~JsonHolder() override = default; | ||
|
||
static Status Make(const FunctionNode& node, std::shared_ptr<JsonHolder>* holder); | ||
static Status Make(std::shared_ptr<JsonHolder>* holder); | ||
|
||
//TODO(): should try to return const uint8_t * | ||
const uint8_t* operator()(const std::string& json_str, const std::string& json_path, int32_t* out_len); | ||
|
||
arrow::json::ParseOptions parse_options_ = arrow::json::ParseOptions::Defaults(); | ||
arrow::json::ReadOptions read_options_ = arrow::json::ReadOptions::Defaults(); | ||
}; | ||
|
||
} // namespace gandiva |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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 "gandiva/json_holder.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include "gandiva/regex_util.h" | ||
|
||
namespace gandiva { | ||
|
||
class TestJsonHolder : public ::testing::Test {}; | ||
|
||
TEST_F(TestJsonHolder, TestJson) { | ||
std::shared_ptr<JsonHolder> json_holder; | ||
|
||
auto status = JsonHolder::Make(&json_holder); | ||
EXPECT_EQ(status.ok(), true) << status.message(); | ||
|
||
auto& get_json_object = *json_holder; | ||
|
||
int32_t out_len; | ||
const uint8_t* data = get_json_object(R"({"hello": 3.5 })", "$.hello", &out_len); | ||
EXPECT_EQ(std::string((char*)data, out_len), "3.5"); | ||
|
||
} | ||
|
||
} // namespace gandiva |