Skip to content

Commit 51149a8

Browse files
weizuo93weizuo
andauthored
[Function] Support function aes_encrypt and aes_decrypt on vectorized engine (apache#184)
Co-authored-by: weizuo <[email protected]>
1 parent 32ad47b commit 51149a8

3 files changed

Lines changed: 157 additions & 4 deletions

File tree

‎be/src/vec/functions/function_string.cpp‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "vec/functions/function_totype.h"
3030
#include "vec/functions/simple_function_factory.h"
3131

32+
#include "util/aes_util.h"
3233
#include "util/url_coding.h"
3334

3435
namespace doris::vectorized {
@@ -389,6 +390,88 @@ struct StringSpace {
389390
}
390391
};
391392

393+
394+
struct AesEncryptImpl {
395+
static constexpr auto name = "aes_encrypt";
396+
using Chars = ColumnString::Chars;
397+
using Offsets = ColumnString::Offsets;
398+
using ReturnType = DataTypeString;
399+
using ColumnType = ColumnString;
400+
static void vector_vector(const Chars& ldata, const Offsets& loffsets, const Chars& rdata,
401+
const Offsets& roffsets, Chars& res_data, Offsets& res_offsets,
402+
NullMap& null_map_data) {
403+
DCHECK_EQ(loffsets.size(), roffsets.size());
404+
size_t input_rows_count = loffsets.size();
405+
res_offsets.resize(input_rows_count);
406+
407+
for (size_t i = 0; i < input_rows_count; ++i) {
408+
int l_size = loffsets[i] - loffsets[i - 1] - 1;
409+
const auto l_raw = reinterpret_cast<const char*>(&ldata[loffsets[i - 1]]);
410+
411+
int r_size = roffsets[i] - roffsets[i - 1] - 1;
412+
const auto r_raw = reinterpret_cast<const char*>(&rdata[roffsets[i - 1]]);
413+
414+
if (*l_raw == '\0' || l_size == 0) {
415+
StringOP::push_null_string(i, res_data, res_offsets, null_map_data);
416+
continue;
417+
}
418+
419+
int cipher_len = l_size + 16;
420+
char p[cipher_len];
421+
422+
int outlen =
423+
AesUtil::encrypt(AES_128_ECB, (unsigned char*)l_raw, l_size, (unsigned char*)r_raw,
424+
r_size, NULL, true, (unsigned char*)p);
425+
if (outlen < 0) {
426+
StringOP::push_null_string(i, res_data, res_offsets, null_map_data);
427+
} else {
428+
StringOP::push_value_string(std::string_view(p, outlen), i, res_data, res_offsets);
429+
}
430+
}
431+
}
432+
};
433+
434+
struct AesDecryptImpl {
435+
static constexpr auto name = "aes_decrypt";
436+
using Chars = ColumnString::Chars;
437+
using Offsets = ColumnString::Offsets;
438+
using ReturnType = DataTypeString;
439+
using ColumnType = ColumnString;
440+
static void vector_vector(const Chars& ldata, const Offsets& loffsets, const Chars& rdata,
441+
const Offsets& roffsets, Chars& res_data, Offsets& res_offsets,
442+
NullMap& null_map_data) {
443+
DCHECK_EQ(loffsets.size(), roffsets.size());
444+
size_t input_rows_count = loffsets.size();
445+
res_offsets.resize(input_rows_count);
446+
447+
for (size_t i = 0; i < input_rows_count; ++i) {
448+
int l_size = loffsets[i] - loffsets[i - 1] - 1;
449+
const auto l_raw = reinterpret_cast<const char*>(&ldata[loffsets[i - 1]]);
450+
451+
int r_size = roffsets[i] - roffsets[i - 1] - 1;
452+
const auto r_raw = reinterpret_cast<const char*>(&rdata[roffsets[i - 1]]);
453+
454+
if (*l_raw == '\0' || l_size == 0) {
455+
StringOP::push_null_string(i, res_data, res_offsets, null_map_data);
456+
continue;
457+
}
458+
459+
int cipher_len = l_size;
460+
char p[cipher_len];
461+
462+
int outlen =
463+
AesUtil::decrypt(AES_128_ECB, (unsigned char*)l_raw, l_size, (unsigned char*)r_raw,
464+
r_size, NULL, true, (unsigned char*)p);
465+
if (outlen < 0) {
466+
StringOP::push_null_string(i, res_data, res_offsets, null_map_data);
467+
} else {
468+
StringOP::push_value_string(std::string_view(p, outlen), i, res_data, res_offsets);
469+
}
470+
}
471+
}
472+
};
473+
474+
392475
struct ToBase64Impl {
393476
static constexpr auto name = "to_base64";
394477
using ReturnType = DataTypeString;
@@ -557,6 +640,10 @@ using FunctionRTrim = FunctionStringToString<TrimImpl<false, true>, NameRTrim>;
557640

558641
using FunctionTrim = FunctionStringToString<TrimImpl<true, true>, NameTrim>;
559642

643+
using FunctionAesEncrypt = FunctionBinaryStringOperateToNullType<AesEncryptImpl>;
644+
645+
using FunctionAesDecrypt = FunctionBinaryStringOperateToNullType<AesDecryptImpl>;
646+
560647
using FunctionToBase64 = FunctionStringOperateToNullType<ToBase64Impl>;
561648

562649
using FunctionFromBase64 = FunctionStringOperateToNullType<FromBase64Impl>;
@@ -594,6 +681,8 @@ void register_function_string(SimpleFunctionFactory& factory) {
594681
factory.register_function<FunctionStringRepeat>();
595682
factory.register_function<FunctionStringLPad>();
596683
factory.register_function<FunctionStringRPad>();
684+
factory.register_function<FunctionAesEncrypt>();
685+
factory.register_function<FunctionAesDecrypt>();
597686
factory.register_function<FunctionToBase64>();
598687
factory.register_function<FunctionFromBase64>();
599688
factory.register_function<FunctionSplitPart>();

‎be/test/vec/function/function_string_test.cpp‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "function_test_util.h"
2727
#include "runtime/row_batch.h"
2828
#include "runtime/tuple_row.h"
29+
#include "util/aes_util.h"
2930
#include "util/url_coding.h"
3031
#include "vec/core/field.h"
3132
#include "vec/functions/simple_function_factory.h"
@@ -551,6 +552,69 @@ TEST(function_string_test, function_md5sum_test) {
551552
}
552553
}
553554

555+
TEST(function_string_test, function_aes_encrypt_test) {
556+
std::string func_name = "aes_encrypt";
557+
558+
std::vector<std::any> input_types = {TypeIndex::String,
559+
TypeIndex::String};
560+
561+
char* key = "doris";
562+
char* src[6] = {"aaaaaa", "bbbbbb", "cccccc", "dddddd", "eeeeee", ""};
563+
std::string r[5];
564+
565+
for (int i = 0; i < 5; i++) {
566+
567+
int cipher_len = strlen(src[i]) + 16;
568+
char p[cipher_len];
569+
570+
int outlen = AesUtil::encrypt(AES_128_ECB, (unsigned char*)src[i], strlen(src[i]),
571+
(unsigned char*)key, strlen(key), NULL, true, (unsigned char*)p);
572+
r[i] = std::string(p, outlen);
573+
574+
}
575+
576+
DataSet data_set = {{{std::string(src[0]), std::string(key)}, r[0]},
577+
{{std::string(src[1]), std::string(key)}, r[1]},
578+
{{std::string(src[2]), std::string(key)}, r[2]},
579+
{{std::string(src[3]), std::string(key)}, r[3]},
580+
{{std::string(src[4]), std::string(key)}, r[4]},
581+
{{std::string(src[5]), std::string(key)}, Null()},
582+
{{Null(), std::string(key)}, Null()}};
583+
584+
vectorized::check_function<vectorized::DataTypeString, true>(func_name, input_types, data_set);
585+
}
586+
587+
TEST(function_string_test, function_aes_decrypt_test) {
588+
std::string func_name = "aes_decrypt";
589+
590+
std::vector<std::any> input_types = {TypeIndex::String,
591+
TypeIndex::String};
592+
593+
char* key = "doris";
594+
char* src[5] = {"aaaaaa", "bbbbbb", "cccccc", "dddddd", "eeeeee"};
595+
std::string r[5];
596+
597+
for (int i = 0; i < 5; i++) {
598+
599+
int cipher_len = strlen(src[i]) + 16;
600+
char p[cipher_len];
601+
602+
int outlen = AesUtil::encrypt(AES_128_ECB, (unsigned char*)src[i], strlen(src[i]),
603+
(unsigned char*)key, strlen(key), NULL, true, (unsigned char*)p);
604+
r[i] = std::string(p, outlen);
605+
606+
}
607+
608+
DataSet data_set = {{{r[0], std::string(key)}, std::string(src[0])},
609+
{{r[1], std::string(key)}, std::string(src[1])},
610+
{{r[2], std::string(key)}, std::string(src[2])},
611+
{{r[3], std::string(key)}, std::string(src[3])},
612+
{{r[4], std::string(key)}, std::string(src[4])},
613+
{{Null(), std::string(key)}, Null()}};
614+
615+
vectorized::check_function<vectorized::DataTypeString, true>(func_name, input_types, data_set);
616+
}
617+
554618
} // namespace doris
555619

556620
int main(int argc, char** argv) {

‎gensrc/script/doris_builtins_functions.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,19 +1195,19 @@
11951195
# aes and base64 function
11961196
[['aes_encrypt'], 'VARCHAR', ['VARCHAR', 'VARCHAR'],
11971197
'_ZN5doris19EncryptionFunctions11aes_encryptEPN9doris_udf'
1198-
'15FunctionContextERKNS1_9StringValES6_', '', '', '', ''],
1198+
'15FunctionContextERKNS1_9StringValES6_', '', '', 'vec', 'ALWAYS_NULLABLE'],
11991199
[['aes_decrypt'], 'VARCHAR', ['VARCHAR', 'VARCHAR'],
12001200
'_ZN5doris19EncryptionFunctions11aes_decryptEPN9doris_udf'
1201-
'15FunctionContextERKNS1_9StringValES6_', '', '', '', ''],
1201+
'15FunctionContextERKNS1_9StringValES6_', '', '', 'vec', 'ALWAYS_NULLABLE'],
12021202
[['from_base64'], 'VARCHAR', ['VARCHAR'],
12031203
'_ZN5doris19EncryptionFunctions11from_base64EPN9doris_udf'
12041204
'15FunctionContextERKNS1_9StringValE', '', '', 'vec', 'ALWAYS_NULLABLE'],
12051205
[['aes_encrypt'], 'STRING', ['STRING', 'STRING'],
12061206
'_ZN5doris19EncryptionFunctions11aes_encryptEPN9doris_udf'
1207-
'15FunctionContextERKNS1_9StringValES6_', '', '', '', ''],
1207+
'15FunctionContextERKNS1_9StringValES6_', '', '', 'vec', 'ALWAYS_NULLABLE'],
12081208
[['aes_decrypt'], 'STRING', ['STRING', 'STRING'],
12091209
'_ZN5doris19EncryptionFunctions11aes_decryptEPN9doris_udf'
1210-
'15FunctionContextERKNS1_9StringValES6_', '', '', '', ''],
1210+
'15FunctionContextERKNS1_9StringValES6_', '', '', 'vec', 'ALWAYS_NULLABLE'],
12111211
[['from_base64'], 'STRING', ['STRING'],
12121212
'_ZN5doris19EncryptionFunctions11from_base64EPN9doris_udf'
12131213
'15FunctionContextERKNS1_9StringValE', '', '', '', ''],

0 commit comments

Comments
 (0)