generated from duckdb/extension-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathredis_extension.cpp
More file actions
1161 lines (1039 loc) · 48.7 KB
/
redis_extension.cpp
File metadata and controls
1161 lines (1039 loc) · 48.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define DUCKDB_EXTENSION_MAIN
#include "redis_extension.hpp"
#include "duckdb.hpp"
#include "duckdb/common/exception.hpp"
#include "duckdb/function/scalar_function.hpp"
#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
#include "duckdb/parser/parsed_data/create_table_function_info.hpp"
#include "duckdb/function/table_function.hpp"
#include "duckdb/main/extension/extension_loader.hpp"
#include <boost/asio.hpp>
#include <string>
#include <mutex>
#include <unordered_map>
#include <memory>
#include "query_farm_telemetry.hpp"
namespace duckdb {
using boost::asio::ip::tcp;
// Simple Redis protocol formatter
class RedisProtocol {
public:
static std::string formatAuth(const std::string &password) {
return "*2\r\n$4\r\nAUTH\r\n$" + std::to_string(password.length()) + "\r\n" + password + "\r\n";
}
static std::string formatGet(const std::string &key) {
return "*2\r\n$3\r\nGET\r\n$" + std::to_string(key.length()) + "\r\n" + key + "\r\n";
}
static std::string formatSet(const std::string &key, const std::string &value) {
return "*3\r\n$3\r\nSET\r\n$" + std::to_string(key.length()) + "\r\n" + key + "\r\n$" +
std::to_string(value.length()) + "\r\n" + value + "\r\n";
}
static std::string parseResponse(const std::string &response) {
if (response.empty())
return "";
if (response[0] == '$') {
// Bulk string response
size_t pos = response.find("\r\n");
if (pos == std::string::npos)
return "";
// Skip the length prefix and first \r\n
pos += 2;
std::string value = response.substr(pos);
// Remove trailing \r\n if present
if (value.size() >= 2 && value.substr(value.size() - 2) == "\r\n") {
value = value.substr(0, value.size() - 2);
}
return value;
} else if (response[0] == '+') {
// Simple string response
return response.substr(1, response.find("\r\n") - 1);
} else if (response[0] == '-') {
// Error response
throw InvalidInputException("Redis error: " + response.substr(1));
}
return response;
}
// Hash operations
static std::string formatHGet(const std::string &key, const std::string &field) {
return "*3\r\n$4\r\nHGET\r\n$" + std::to_string(key.length()) + "\r\n" + key + "\r\n$" +
std::to_string(field.length()) + "\r\n" + field + "\r\n";
}
static std::string formatHSet(const std::string &key, const std::string &field, const std::string &value) {
return "*4\r\n$4\r\nHSET\r\n$" + std::to_string(key.length()) + "\r\n" + key + "\r\n$" +
std::to_string(field.length()) + "\r\n" + field + "\r\n$" + std::to_string(value.length()) + "\r\n" +
value + "\r\n";
}
static std::string formatHGetAll(const std::string &key) {
return "*2\r\n$7\r\nHGETALL\r\n$" + std::to_string(key.length()) + "\r\n" + key + "\r\n";
}
// List operations
static std::string formatLPush(const std::string &key, const std::string &value) {
return "*3\r\n$5\r\nLPUSH\r\n$" + std::to_string(key.length()) + "\r\n" + key + "\r\n$" +
std::to_string(value.length()) + "\r\n" + value + "\r\n";
}
static std::string formatLRange(const std::string &key, int64_t start, int64_t stop) {
auto start_str = std::to_string(start);
auto stop_str = std::to_string(stop);
return "*4\r\n$6\r\nLRANGE\r\n$" + std::to_string(key.length()) + "\r\n" + key + "\r\n$" +
std::to_string(start_str.length()) + "\r\n" + start_str + "\r\n$" + std::to_string(stop_str.length()) +
"\r\n" + stop_str + "\r\n";
}
// Key scanning
static std::string formatScan(const std::string &cursor, const std::string &pattern = "*", int64_t count = 10) {
std::string cmd = "*6\r\n$4\r\nSCAN\r\n";
cmd += "$" + std::to_string(cursor.length()) + "\r\n" + cursor + "\r\n";
cmd += "$5\r\nMATCH\r\n";
cmd += "$" + std::to_string(pattern.length()) + "\r\n" + pattern + "\r\n";
cmd += "$5\r\nCOUNT\r\n";
auto count_str = std::to_string(count);
cmd += "$" + std::to_string(count_str.length()) + "\r\n" + count_str + "\r\n";
return cmd;
}
static std::string formatHScan(const std::string &key, const std::string &cursor, const std::string &pattern = "*",
int64_t count = 10) {
std::string cmd = "*6\r\n$5\r\nHSCAN\r\n";
cmd += "$" + std::to_string(key.length()) + "\r\n" + key + "\r\n";
cmd += "$" + std::to_string(cursor.length()) + "\r\n" + cursor + "\r\n";
cmd += "$5\r\nMATCH\r\n";
cmd += "$" + std::to_string(pattern.length()) + "\r\n" + pattern + "\r\n";
cmd += "$5\r\nCOUNT\r\n";
auto count_str = std::to_string(count);
cmd += "$" + std::to_string(count_str.length()) + "\r\n" + count_str + "\r\n";
return cmd;
}
static std::vector<std::string> parseArrayResponse(const std::string &response) {
std::vector<std::string> result;
if (response.empty() || response[0] != '*')
return result;
size_t pos = 1;
size_t end = response.find("\r\n", pos);
int array_size = std::stoi(response.substr(pos, end - pos));
pos = end + 2;
for (int i = 0; i < array_size; i++) {
if (response[pos] == '$') {
pos++;
end = response.find("\r\n", pos);
int str_len = std::stoi(response.substr(pos, end - pos));
pos = end + 2;
if (str_len >= 0) {
result.push_back(response.substr(pos, str_len));
pos += str_len + 2;
}
}
}
return result;
}
static std::string formatMGet(const std::vector<std::string> &keys) {
std::string cmd = "*" + std::to_string(keys.size() + 1) + "\r\n$4\r\nMGET\r\n";
for (const auto &key : keys) {
cmd += "$" + std::to_string(key.length()) + "\r\n" + key + "\r\n";
}
return cmd;
}
static std::string formatExpire(const std::string &key, int64_t seconds) {
return "*3\r\n$6\r\nEXPIRE\r\n$" + std::to_string(key.length()) + "\r\n" + key + "\r\n$" +
std::to_string(std::to_string(seconds).length()) + "\r\n" + std::to_string(seconds) + "\r\n";
}
static std::string formatExpireAt(const std::string &key, int64_t timestamp) {
return "*3\r\n$8\r\nEXPIREAT\r\n$" + std::to_string(key.length()) + "\r\n" + key + "\r\n$" +
std::to_string(std::to_string(timestamp).length()) + "\r\n" + std::to_string(timestamp) + "\r\n";
}
static std::string formatTtl(const std::string &key) {
return "*2\r\n$3\r\nTTL\r\n$" + std::to_string(key.length()) + "\r\n" + key + "\r\n";
}
static int64_t parseIntegerResponse(const std::string &response) {
if (response.empty() || response[0] != ':') {
throw InvalidInputException("Invalid Redis integer response");
}
size_t end = response.find("\r\n");
if (end == std::string::npos) {
throw InvalidInputException("Invalid Redis integer response");
}
try {
return std::stoll(response.substr(1, end - 1));
} catch (const std::exception &e) {
throw InvalidInputException("Failed to parse Redis integer response: %s", e.what());
}
}
};
// Redis connection class
class RedisConnection {
public:
RedisConnection(const std::string &host, const std::string &port, const std::string &password = "")
: io_context_(), socket_(io_context_) {
try {
tcp::resolver resolver(io_context_);
auto endpoints = resolver.resolve(host, port);
boost::asio::connect(socket_, endpoints);
if (!password.empty()) {
std::string auth_cmd = RedisProtocol::formatAuth(password);
boost::asio::write(socket_, boost::asio::buffer(auth_cmd));
boost::asio::streambuf response;
boost::asio::read_until(socket_, response, "\r\n");
std::string auth_response((std::istreambuf_iterator<char>(&response)),
std::istreambuf_iterator<char>());
RedisProtocol::parseResponse(auth_response);
}
} catch (std::exception &e) {
throw InvalidInputException("Redis connection error: " + std::string(e.what()));
}
}
std::string execute(const std::string &command) {
std::lock_guard<std::mutex> lock(mutex_);
try {
boost::asio::write(socket_, boost::asio::buffer(command));
boost::asio::streambuf response;
boost::asio::read_until(socket_, response, "\r\n");
return std::string((std::istreambuf_iterator<char>(&response)), std::istreambuf_iterator<char>());
} catch (std::exception &e) {
throw InvalidInputException("Redis execution error: " + std::string(e.what()));
}
}
private:
boost::asio::io_context io_context_;
tcp::socket socket_;
std::mutex mutex_;
};
// Connection pool manager
class ConnectionPool {
public:
static ConnectionPool &getInstance() {
static ConnectionPool instance;
return instance;
}
std::shared_ptr<RedisConnection> getConnection(const std::string &host, const std::string &port,
const std::string &password = "") {
std::string key = host + ":" + port;
std::lock_guard<std::mutex> lock(mutex_);
auto it = connections_.find(key);
if (it == connections_.end()) {
auto conn = std::make_shared<RedisConnection>(host, port, password);
connections_[key] = conn;
return conn;
}
return it->second;
}
private:
ConnectionPool() {
}
std::mutex mutex_;
std::unordered_map<std::string, std::shared_ptr<RedisConnection>> connections_;
};
// Add this helper function
static bool GetRedisSecret(ClientContext &context, const string &secret_name, string &host, string &port,
string &password) {
auto &secret_manager = SecretManager::Get(context);
try {
auto transaction = CatalogTransaction::GetSystemCatalogTransaction(context);
auto secret_match = secret_manager.LookupSecret(transaction, secret_name, "redis");
if (secret_match.HasMatch()) {
auto &secret = secret_match.GetSecret();
if (secret.GetType() != "redis") {
throw InvalidInputException("Invalid secret type. Expected 'redis', got '%s'", secret.GetType());
}
const auto *kv_secret = dynamic_cast<const KeyValueSecret *>(&secret);
if (!kv_secret) {
throw InvalidInputException("Invalid secret format for 'redis' secret");
}
Value host_val, port_val, password_val;
if (!kv_secret->TryGetValue("host", host_val) || !kv_secret->TryGetValue("port", port_val) ||
!kv_secret->TryGetValue("password", password_val)) {
return false;
}
host = host_val.ToString();
port = port_val.ToString();
password = password_val.ToString();
return true;
}
} catch (...) {
return false;
}
return false;
}
// Modify the function signatures to accept secret name instead of connection details
static void RedisGetFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &secret_vector = args.data[1];
UnaryExecutor::Execute<string_t, string_t>(key_vector, result, args.size(), [&](string_t key) {
try {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
auto response = conn->execute(RedisProtocol::formatGet(key.GetString()));
return StringVector::AddString(result, RedisProtocol::parseResponse(response));
} catch (std::exception &e) {
throw InvalidInputException("Redis GET error: %s", e.what());
}
});
}
static void RedisSetFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &value_vector = args.data[1];
auto &secret_vector = args.data[2];
BinaryExecutor::Execute<string_t, string_t, string_t>(
key_vector, value_vector, result, args.size(), [&](string_t key, string_t value) {
try {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
auto response = conn->execute(RedisProtocol::formatSet(key.GetString(), value.GetString()));
return StringVector::AddString(result, RedisProtocol::parseResponse(response));
} catch (std::exception &e) {
throw InvalidInputException("Redis SET error: %s", e.what());
}
});
}
// Hash operations
static void RedisHGetFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &field_vector = args.data[1];
auto &secret_vector = args.data[2];
BinaryExecutor::Execute<string_t, string_t, string_t>(
key_vector, field_vector, result, args.size(), [&](string_t key, string_t field) {
try {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
auto response = conn->execute(RedisProtocol::formatHGet(key.GetString(), field.GetString()));
return StringVector::AddString(result, RedisProtocol::parseResponse(response));
} catch (std::exception &e) {
throw InvalidInputException("Redis HGET error: %s", e.what());
}
});
}
static void RedisHSetFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &field_vector = args.data[1];
auto &value_vector = args.data[2];
auto &secret_vector = args.data[3];
BinaryExecutor::Execute<string_t, string_t, string_t>(
key_vector, field_vector, result, args.size(), [&](string_t key, string_t field) {
try {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
auto response = conn->execute(
RedisProtocol::formatHSet(key.GetString(), field.GetString(), value_vector.GetValue(0).ToString()));
return StringVector::AddString(result, RedisProtocol::parseResponse(response));
} catch (std::exception &e) {
throw InvalidInputException("Redis HSET error: %s", e.what());
}
});
}
// List operations
static void RedisLPushFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &value_vector = args.data[1];
auto &secret_vector = args.data[2];
BinaryExecutor::Execute<string_t, string_t, string_t>(
key_vector, value_vector, result, args.size(), [&](string_t key, string_t value) {
try {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
auto response = conn->execute(RedisProtocol::formatLPush(key.GetString(), value.GetString()));
return StringVector::AddString(result, RedisProtocol::parseResponse(response));
} catch (std::exception &e) {
throw InvalidInputException("Redis LPUSH error: %s", e.what());
}
});
}
static void RedisLRangeFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &start_vector = args.data[1];
auto &stop_vector = args.data[2];
auto &secret_vector = args.data[3];
BinaryExecutor::Execute<string_t, int64_t, string_t>(
key_vector, start_vector, result, args.size(), [&](string_t key, int64_t start) {
try {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
auto stop = stop_vector.GetValue(0).GetValue<int64_t>();
auto response = conn->execute(RedisProtocol::formatLRange(key.GetString(), start, stop));
auto values = RedisProtocol::parseArrayResponse(response);
// Join array values with comma for string result
std::string joined;
for (size_t i = 0; i < values.size(); i++) {
if (i > 0)
joined += ",";
joined += values[i];
}
return StringVector::AddString(result, joined);
} catch (std::exception &e) {
throw InvalidInputException("Redis LRANGE error: %s", e.what());
}
});
}
static void RedisMGetFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &keys_list = args.data[0];
auto &secret_vector = args.data[1];
UnaryExecutor::Execute<string_t, string_t>(keys_list, result, args.size(), [&](string_t keys_str) {
try {
// Split comma-separated keys
std::vector<std::string> keys;
std::string key_list = keys_str.GetString();
size_t pos = 0;
while ((pos = key_list.find(',')) != std::string::npos) {
keys.push_back(key_list.substr(0, pos));
key_list.erase(0, pos + 1);
}
if (!key_list.empty()) {
keys.push_back(key_list);
}
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
auto response = conn->execute(RedisProtocol::formatMGet(keys));
auto values = RedisProtocol::parseArrayResponse(response);
// Join results with comma
std::string joined;
for (size_t i = 0; i < values.size(); i++) {
if (i > 0)
joined += ",";
joined += values[i];
}
return StringVector::AddString(result, joined);
} catch (std::exception &e) {
throw InvalidInputException("Redis MGET error: %s", e.what());
}
});
}
static void RedisScanFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &cursor_vector = args.data[0];
auto &pattern_vector = args.data[1];
auto &count_vector = args.data[2];
auto &secret_vector = args.data[3];
BinaryExecutor::Execute<string_t, string_t, string_t>(
cursor_vector, pattern_vector, result, args.size(), [&](string_t cursor, string_t pattern) {
try {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto count = count_vector.GetValue(0).GetValue<int64_t>();
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
auto response =
conn->execute(RedisProtocol::formatScan(cursor.GetString(), pattern.GetString(), count));
auto scan_result = RedisProtocol::parseArrayResponse(response);
if (scan_result.size() >= 2) {
// First element is the new cursor, second element is array of keys
std::string result_str = scan_result[0] + ":";
auto keys = RedisProtocol::parseArrayResponse(scan_result[1]);
for (size_t i = 0; i < keys.size(); i++) {
if (i > 0)
result_str += ",";
result_str += keys[i];
}
return StringVector::AddString(result, result_str);
}
return StringVector::AddString(result, "0:");
} catch (std::exception &e) {
throw InvalidInputException("Redis SCAN error: %s", e.what());
}
});
}
static void RedisHScanFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &cursor_vector = args.data[1];
auto &pattern_vector = args.data[2];
auto &count_vector = args.data[3];
auto &secret_vector = args.data[4];
BinaryExecutor::Execute<string_t, string_t, string_t>(
key_vector, cursor_vector, result, args.size(), [&](string_t key, string_t cursor) {
try {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto pattern = pattern_vector.GetValue(0).ToString();
auto count = count_vector.GetValue(0).GetValue<int64_t>();
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
auto response =
conn->execute(RedisProtocol::formatHScan(key.GetString(), cursor.GetString(), pattern, count));
// HSCAN returns [cursor, [field1, value1, field2, value2, ...]]
auto scan_result = RedisProtocol::parseArrayResponse(response);
std::string result_str;
if (scan_result.size() >= 2) {
result_str = scan_result[0] + ":";
auto kvs = RedisProtocol::parseArrayResponse(scan_result[1]);
for (size_t i = 0; i < kvs.size(); i += 2) {
if (i > 0)
result_str += ",";
result_str += kvs[i] + "=" + ((i + 1) < kvs.size() ? kvs[i + 1] : "");
}
} else {
result_str = "0:";
}
return StringVector::AddString(result, result_str);
} catch (std::exception &e) {
throw InvalidInputException("Redis HSCAN error: %s", e.what());
}
});
}
struct RedisHScanOverScanBindData : public duckdb::TableFunctionData {
std::string scan_pattern;
std::string hscan_pattern;
int64_t count;
std::string secret_name;
// State for SCAN
std::string scan_cursor = "0";
std::vector<std::string> scan_keys;
size_t scan_key_idx = 0;
// State for HSCAN
std::string hscan_cursor = "0";
std::vector<std::string> hscan_kvs;
size_t hscan_kv_idx = 0;
// Connection
std::shared_ptr<RedisConnection> conn;
std::string host, port, password;
bool scan_complete = false;
};
static duckdb::unique_ptr<duckdb::FunctionData>
RedisHScanOverScanBind(duckdb::ClientContext &context, duckdb::TableFunctionBindInput &input,
duckdb::vector<duckdb::LogicalType> &return_types, duckdb::vector<std::string> &names) {
auto result = duckdb::make_uniq<RedisHScanOverScanBindData>();
result->scan_pattern = input.inputs[0].ToString();
result->hscan_pattern = input.inputs[1].ToString();
result->count = input.inputs[2].GetValue<int64_t>();
result->secret_name = input.inputs[3].ToString();
// Output columns
return_types = {duckdb::LogicalType::VARCHAR, duckdb::LogicalType::VARCHAR, duckdb::LogicalType::VARCHAR};
names = {"key", "field", "value"};
// Get connection info
if (!GetRedisSecret(context, result->secret_name, result->host, result->port, result->password)) {
throw duckdb::InvalidInputException("Redis secret not found");
}
result->conn = ConnectionPool::getInstance().getConnection(result->host, result->port, result->password);
return std::move(result);
}
static void RedisHScanOverScanFunction(duckdb::ClientContext &context, duckdb::TableFunctionInput &data_p,
duckdb::DataChunk &output) {
auto &data = (RedisHScanOverScanBindData &)*data_p.bind_data;
idx_t out_row = 0;
const idx_t max_rows = output.size();
while (out_row < max_rows) {
// If we have hscan_kvs buffered, emit them
while (data.hscan_kv_idx + 1 < data.hscan_kvs.size() && out_row < max_rows) {
// hscan_kvs = [field1, value1, field2, value2, ...]
output.SetValue(0, out_row, duckdb::Value(data.scan_keys[data.scan_key_idx]));
output.SetValue(1, out_row, duckdb::Value(data.hscan_kvs[data.hscan_kv_idx]));
output.SetValue(2, out_row, duckdb::Value(data.hscan_kvs[data.hscan_kv_idx + 1]));
data.hscan_kv_idx += 2;
out_row++;
}
// If we exhausted hscan_kvs, move to next key
if (data.hscan_kv_idx >= data.hscan_kvs.size() && data.scan_key_idx < data.scan_keys.size()) {
// Next key
data.hscan_kv_idx = 0;
data.hscan_kvs.clear();
data.hscan_cursor = "0";
// HSCAN for this key
std::string hscan_resp = data.conn->execute(RedisProtocol::formatHScan(
data.scan_keys[data.scan_key_idx], data.hscan_cursor, data.hscan_pattern, data.count));
auto hscan_result = RedisProtocol::parseArrayResponse(hscan_resp);
if (hscan_result.size() >= 2) {
data.hscan_cursor = hscan_result[0];
data.hscan_kvs = RedisProtocol::parseArrayResponse(hscan_result[1]);
}
// If no fields, skip to next key
if (data.hscan_kvs.empty()) {
data.scan_key_idx++;
continue;
}
// Otherwise, emit from hscan_kvs
continue;
}
// If we exhausted keys, SCAN for more
if (data.scan_key_idx >= data.scan_keys.size() && !data.scan_complete) {
std::string scan_resp =
data.conn->execute(RedisProtocol::formatScan(data.scan_cursor, data.scan_pattern, data.count));
auto scan_result = RedisProtocol::parseArrayResponse(scan_resp);
if (scan_result.size() >= 2) {
data.scan_cursor = scan_result[0];
data.scan_keys = RedisProtocol::parseArrayResponse(scan_result[1]);
data.scan_key_idx = 0;
if (data.scan_cursor == "0") {
data.scan_complete = true;
}
} else {
data.scan_complete = true;
break;
}
continue;
}
// If all done, break
if ((data.scan_complete && data.scan_key_idx >= data.scan_keys.size()) || out_row == 0) {
break;
}
}
output.SetCardinality(out_row);
}
// Table function: redis_keys(pattern, secret_name) -> (key VARCHAR)
struct RedisKeysBindData : public duckdb::TableFunctionData {
std::string pattern;
std::string secret_name;
std::string host, port, password;
std::shared_ptr<RedisConnection> conn;
std::string cursor = "0";
std::vector<std::string> keys;
size_t key_idx = 0;
bool scan_complete = false;
};
static duckdb::unique_ptr<duckdb::FunctionData> RedisKeysBind(duckdb::ClientContext &context,
duckdb::TableFunctionBindInput &input,
duckdb::vector<duckdb::LogicalType> &return_types,
duckdb::vector<std::string> &names) {
auto result = duckdb::make_uniq<RedisKeysBindData>();
result->pattern = input.inputs[0].ToString();
result->secret_name = input.inputs[1].ToString();
return_types = {duckdb::LogicalType::VARCHAR};
names = {"key"};
if (!GetRedisSecret(context, result->secret_name, result->host, result->port, result->password)) {
throw duckdb::InvalidInputException("Redis secret not found");
}
result->conn = ConnectionPool::getInstance().getConnection(result->host, result->port, result->password);
return std::move(result);
}
static void RedisKeysFunction(duckdb::ClientContext &context, duckdb::TableFunctionInput &data_p,
duckdb::DataChunk &output) {
auto &data = (RedisKeysBindData &)*data_p.bind_data;
idx_t out_row = 0;
const idx_t max_rows = output.size();
while (out_row < max_rows) {
while (data.key_idx < data.keys.size() && out_row < max_rows) {
output.SetValue(0, out_row, duckdb::Value(data.keys[data.key_idx]));
data.key_idx++;
out_row++;
}
if (data.key_idx >= data.keys.size() && !data.scan_complete) {
std::string scan_resp = data.conn->execute(RedisProtocol::formatScan(data.cursor, data.pattern, 100));
auto scan_result = RedisProtocol::parseArrayResponse(scan_resp);
if (scan_result.size() >= 2) {
data.cursor = scan_result[0];
data.keys = RedisProtocol::parseArrayResponse(scan_result[1]);
data.key_idx = 0;
if (data.cursor == "0") {
data.scan_complete = true;
}
} else {
data.scan_complete = true;
break;
}
continue;
}
if (data.scan_complete && data.key_idx >= data.keys.size()) {
break;
}
}
output.SetCardinality(out_row);
}
// Table function: redis_hgetall(key, secret_name) -> (field VARCHAR, value VARCHAR)
struct RedisHGetAllBindData : public duckdb::TableFunctionData {
std::string key;
std::string secret_name;
std::string host, port, password;
std::shared_ptr<RedisConnection> conn;
std::vector<std::string> kvs;
size_t kv_idx = 0;
bool fetched = false;
};
static duckdb::unique_ptr<duckdb::FunctionData> RedisHGetAllBind(duckdb::ClientContext &context,
duckdb::TableFunctionBindInput &input,
duckdb::vector<duckdb::LogicalType> &return_types,
duckdb::vector<std::string> &names) {
auto result = duckdb::make_uniq<RedisHGetAllBindData>();
result->key = input.inputs[0].ToString();
result->secret_name = input.inputs[1].ToString();
return_types = {duckdb::LogicalType::VARCHAR, duckdb::LogicalType::VARCHAR};
names = {"field", "value"};
if (!GetRedisSecret(context, result->secret_name, result->host, result->port, result->password)) {
throw duckdb::InvalidInputException("Redis secret not found");
}
result->conn = ConnectionPool::getInstance().getConnection(result->host, result->port, result->password);
return std::move(result);
}
static void RedisHGetAllFunction(duckdb::ClientContext &context, duckdb::TableFunctionInput &data_p,
duckdb::DataChunk &output) {
auto &data = (RedisHGetAllBindData &)*data_p.bind_data;
if (!data.fetched) {
std::string resp = data.conn->execute(RedisProtocol::formatHGetAll(data.key));
data.kvs = RedisProtocol::parseArrayResponse(resp);
data.kv_idx = 0;
data.fetched = true;
}
idx_t out_row = 0;
const idx_t max_rows = output.size();
while (data.kv_idx + 1 < data.kvs.size() && out_row < max_rows) {
output.SetValue(0, out_row, duckdb::Value(data.kvs[data.kv_idx]));
output.SetValue(1, out_row, duckdb::Value(data.kvs[data.kv_idx + 1]));
data.kv_idx += 2;
out_row++;
}
output.SetCardinality(out_row);
}
// Table function: redis_lrange_table(key, start, stop, secret_name) -> (index BIGINT, value VARCHAR)
struct RedisLRangeTableBindData : public duckdb::TableFunctionData {
std::string key;
int64_t start;
int64_t stop;
std::string secret_name;
std::string host, port, password;
std::shared_ptr<RedisConnection> conn;
std::vector<std::string> values;
size_t idx = 0;
bool fetched = false;
};
static duckdb::unique_ptr<duckdb::FunctionData> RedisLRangeTableBind(duckdb::ClientContext &context,
duckdb::TableFunctionBindInput &input,
duckdb::vector<duckdb::LogicalType> &return_types,
duckdb::vector<std::string> &names) {
auto result = duckdb::make_uniq<RedisLRangeTableBindData>();
result->key = input.inputs[0].ToString();
result->start = input.inputs[1].GetValue<int64_t>();
result->stop = input.inputs[2].GetValue<int64_t>();
result->secret_name = input.inputs[3].ToString();
return_types = {duckdb::LogicalType::BIGINT, duckdb::LogicalType::VARCHAR};
names = {"index", "value"};
if (!GetRedisSecret(context, result->secret_name, result->host, result->port, result->password)) {
throw duckdb::InvalidInputException("Redis secret not found");
}
result->conn = ConnectionPool::getInstance().getConnection(result->host, result->port, result->password);
return std::move(result);
}
static void RedisLRangeTableFunction(duckdb::ClientContext &context, duckdb::TableFunctionInput &data_p,
duckdb::DataChunk &output) {
auto &data = (RedisLRangeTableBindData &)*data_p.bind_data;
if (!data.fetched) {
std::string resp = data.conn->execute(RedisProtocol::formatLRange(data.key, data.start, data.stop));
data.values = RedisProtocol::parseArrayResponse(resp);
data.idx = 0;
data.fetched = true;
}
idx_t out_row = 0;
const idx_t max_rows = output.size();
for (; data.idx < data.values.size() && out_row < max_rows; data.idx++, out_row++) {
output.SetValue(0, out_row, duckdb::Value::BIGINT(data.idx));
output.SetValue(1, out_row, duckdb::Value(data.values[data.idx]));
}
output.SetCardinality(out_row);
}
// Scalar function: redis_del(key, secret_name) -> BOOLEAN
static void RedisDelFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &secret_vector = args.data[1];
UnaryExecutor::Execute<string_t, bool>(key_vector, result, args.size(), [&](string_t key) {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
std::string cmd =
"*2\r\n$3\r\nDEL\r\n$" + std::to_string(key.GetString().length()) + "\r\n" + key.GetString() + "\r\n";
auto resp = conn->execute(cmd);
// DEL returns :1\r\n if deleted, :0\r\n if not
return resp.find(":1\r\n") != std::string::npos;
});
}
// Scalar function: redis_exists(key, secret_name) -> BOOLEAN
static void RedisExistsFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &secret_vector = args.data[1];
UnaryExecutor::Execute<string_t, bool>(key_vector, result, args.size(), [&](string_t key) {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
std::string cmd =
"*2\r\n$6\r\nEXISTS\r\n$" + std::to_string(key.GetString().length()) + "\r\n" + key.GetString() + "\r\n";
auto resp = conn->execute(cmd);
// EXISTS returns :1\r\n if exists, :0\r\n if not
return resp.find(":1\r\n") != std::string::npos;
});
}
// Scalar function: redis_type(key, secret_name) -> VARCHAR
static void RedisTypeFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &secret_vector = args.data[1];
UnaryExecutor::Execute<string_t, string_t>(key_vector, result, args.size(), [&](string_t key) {
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
std::string cmd =
"*2\r\n$4\r\nTYPE\r\n$" + std::to_string(key.GetString().length()) + "\r\n" + key.GetString() + "\r\n";
auto resp = conn->execute(cmd);
// TYPE returns +type\r\n
if (resp.size() > 1 && resp[0] == '+') {
auto end = resp.find("\r\n");
if (end != std::string::npos)
return string_t(resp.substr(1, end - 1));
}
return string_t("");
});
}
static void RedisExpireFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &seconds_vector = args.data[1];
auto &secret_vector = args.data[2];
// Extract secret once before executor loop (optimization)
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
BinaryExecutor::Execute<string_t, int64_t, bool>(
key_vector, seconds_vector, result, args.size(), [&](string_t key, int64_t seconds) {
try {
auto response = conn->execute(RedisProtocol::formatExpire(key.GetString(), seconds));
auto result_int = RedisProtocol::parseIntegerResponse(response);
// Returns 1 if TTL was set (key exists), 0 if key doesn't exist
return result_int == 1;
} catch (std::exception &e) {
throw InvalidInputException("Redis EXPIRE error: %s", e.what());
}
});
}
static void RedisTTLFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto &secret_vector = args.data[1];
// Extract secret once before executor loop (optimization)
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
UnaryExecutor::Execute<string_t, int64_t>(key_vector, result, args.size(), [&](string_t key) {
try {
auto response = conn->execute(RedisProtocol::formatTtl(key.GetString()));
// Returns: -2 if key doesn't exist, -1 if key has no expiry, or positive TTL in seconds
return RedisProtocol::parseIntegerResponse(response);
} catch (std::exception &e) {
throw InvalidInputException("Redis TTL error: %s", e.what());
}
});
}
static void RedisExpireAtFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &key_vector = args.data[0];
auto ×tamp_vector = args.data[1];
auto &secret_vector = args.data[2];
// Extract secret once before executor loop (optimization)
string host, port, password;
if (!GetRedisSecret(state.GetContext(), secret_vector.GetValue(0).ToString(), host, port, password)) {
throw InvalidInputException("Redis secret not found");
}
auto conn = ConnectionPool::getInstance().getConnection(host, port, password);
BinaryExecutor::Execute<string_t, int64_t, bool>(
key_vector, timestamp_vector, result, args.size(), [&](string_t key, int64_t timestamp) {
try {
auto response = conn->execute(RedisProtocol::formatExpireAt(key.GetString(), timestamp));
auto result_int = RedisProtocol::parseIntegerResponse(response);
// Returns 1 if expiry was set (key exists), 0 if key doesn't exist
return result_int == 1;
} catch (std::exception &e) {
throw InvalidInputException("Redis EXPIREAT error: %s", e.what());
}
});
}
static void LoadInternal(ExtensionLoader &loader) {
// Register the secret functions first!
CreateRedisSecretFunctions::Register(loader);
// Helper to add description to a scalar function
auto add_scalar_function = [&](ScalarFunction func, const string &description, const vector<string> ¶m_names,
const vector<string> &examples, const string &category = "redis") {
CreateScalarFunctionInfo info(std::move(func));
FunctionDescription func_desc;
func_desc.description = description;
func_desc.parameter_names = param_names;
func_desc.examples = examples;
func_desc.categories = {category};
info.descriptions.push_back(std::move(func_desc));
loader.RegisterFunction(std::move(info));
};
// Helper to add description to a table function
auto add_table_function = [&](TableFunction func, const string &description, const vector<string> ¶m_names,
const vector<string> &examples, const string &category = "redis") {
CreateTableFunctionInfo info(std::move(func));
FunctionDescription func_desc;
func_desc.description = description;
func_desc.parameter_names = param_names;
func_desc.examples = examples;
func_desc.categories = {category};
info.descriptions.push_back(std::move(func_desc));
loader.RegisterFunction(std::move(info));
};
// Register redis_get scalar function
add_scalar_function(ScalarFunction("redis_get", {LogicalType::VARCHAR, LogicalType::VARCHAR}, LogicalType::VARCHAR,
RedisGetFunction),
"Get the value of a key from Redis. Returns the value associated with the specified key, or an "
"empty string if the key does not exist.",
{"key", "secret_name"},
{"SELECT redis_get('mykey', 'my_redis_secret');",
"SELECT redis_get(key_column, 'my_redis_secret') FROM my_table;"});
// Register redis_set scalar function
add_scalar_function(ScalarFunction("redis_set", {LogicalType::VARCHAR, LogicalType::VARCHAR, LogicalType::VARCHAR},
LogicalType::VARCHAR, RedisSetFunction),
"Set the value of a key in Redis. Returns 'OK' on success.", {"key", "value", "secret_name"},
{"SELECT redis_set('mykey', 'myvalue', 'my_redis_secret');"});
// Register redis_hget scalar function
add_scalar_function(ScalarFunction("redis_hget", {LogicalType::VARCHAR, LogicalType::VARCHAR, LogicalType::VARCHAR},
LogicalType::VARCHAR, RedisHGetFunction),
"Get the value of a field in a Redis hash. Returns the value associated with the field, or an "
"empty string if the field does not exist.",
{"key", "field", "secret_name"}, {"SELECT redis_hget('myhash', 'field1', 'my_redis_secret');"});