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.
Add translate expression support (apache#68)
* Initial commit * Introduce TranslateHolder * Remove unused header
- Loading branch information
1 parent
7e5ae02
commit 7f40757
Showing
8 changed files
with
192 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,62 @@ | ||
// 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/translate_holder.h" | ||
|
||
#include <unordered_map> | ||
#include "gandiva/node.h" | ||
|
||
namespace gandiva { | ||
|
||
Status TranslateHolder::Make(const FunctionNode& node, std::shared_ptr<TranslateHolder>* holder) { | ||
return Make(holder); | ||
} | ||
|
||
Status TranslateHolder::Make(std::shared_ptr<TranslateHolder>* holder) { | ||
*holder = std::shared_ptr<TranslateHolder>(new TranslateHolder()); | ||
return Status::OK(); | ||
} | ||
|
||
const uint8_t* TranslateHolder::operator()(gandiva::ExecutionContext* ctx, std::string text, | ||
std::string matching_str, std::string replace_str, int32_t* out_len) { | ||
char res[text.length()]; | ||
std::unordered_map<char, char> replace_map; | ||
for (int i = 0; i < matching_str.length(); i++) { | ||
if (i >= replace_str.length()) { | ||
replace_map[matching_str[i]] = '\0'; | ||
} else { | ||
replace_map[matching_str[i]] = replace_str[i]; | ||
} | ||
} | ||
int j = 0; | ||
for (int i = 0; i < text.length(); i++) { | ||
if (replace_map.find(text[i]) == replace_map.end()) { | ||
res[j++] = text[i]; | ||
continue; | ||
} | ||
char replace_char = replace_map[text[i]]; | ||
if (replace_char != '\0') { | ||
res[j++] = replace_char; | ||
} | ||
} | ||
*out_len = j; | ||
auto result_buffer = reinterpret_cast<uint8_t*>(ctx->arena()->Allocate(*out_len)); | ||
memcpy(result_buffer, std::string((char*)res, *out_len).data(), *out_len); | ||
return result_buffer; | ||
} | ||
|
||
} // 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,44 @@ | ||
// 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/status.h" | ||
#include "gandiva/execution_context.h" | ||
#include "gandiva/function_holder.h" | ||
#include "gandiva/node.h" | ||
#include "gandiva/visibility.h" | ||
|
||
namespace gandiva { | ||
|
||
/// Function Holder for SQL 'translate' | ||
class GANDIVA_EXPORT TranslateHolder : public FunctionHolder { | ||
public: | ||
TranslateHolder() {} | ||
~TranslateHolder() override = default; | ||
|
||
static Status Make(const FunctionNode& node, std::shared_ptr<TranslateHolder>* holder); | ||
static Status Make(std::shared_ptr<TranslateHolder>* holder); | ||
|
||
const uint8_t* operator()(gandiva::ExecutionContext* ctx, std::string text, | ||
std::string matching_str, std::string replace_str, int32_t* 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,46 @@ | ||
// 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/translate_holder.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace gandiva { | ||
class TestTranslateHolder : public ::testing::Test { | ||
protected: | ||
ExecutionContext ctx_; | ||
}; | ||
|
||
TEST_F(TestTranslateHolder, TestTranslate) { | ||
std::shared_ptr<TranslateHolder> translate_holder; | ||
|
||
auto status = TranslateHolder::Make(&translate_holder); | ||
EXPECT_EQ(status.ok(), true) << status.message(); | ||
|
||
auto translate = *translate_holder; | ||
|
||
int32_t out_len; | ||
const uint8_t* out_str; | ||
|
||
out_str = translate(&ctx_, "ab[cd]", "[]", "", &out_len); | ||
EXPECT_EQ(std::string((char*)out_str, out_len), "abcd"); | ||
|
||
out_str = translate(&ctx_, "ab[cd]", "[]", "#", &out_len); | ||
EXPECT_EQ(std::string((char*)out_str, out_len), "ab#cd"); | ||
} | ||
|
||
} // namespace gandiva |