Skip to content

Commit 0e88222

Browse files
authored
Merge pull request #2 from quackscience/resecrets
Secrets Support for Redis Client
2 parents 75a2668 + a17d73c commit 0e88222

8 files changed

+284
-279
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ include_directories(
1515
${Boost_INCLUDE_DIRS}
1616
)
1717

18-
set(EXTENSION_SOURCES src/redis_extension.cpp)
18+
# Add redis_secret.cpp to the sources
19+
set(EXTENSION_SOURCES
20+
src/redis_extension.cpp
21+
src/redis_secret.cpp
22+
)
1923

2024
build_static_extension(${TARGET_NAME} ${EXTENSION_SOURCES})
2125
build_loadable_extension(${TARGET_NAME} " " ${EXTENSION_SOURCES})

docs/README.md

Lines changed: 86 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -7,234 +7,127 @@ This extension provides Redis client functionality for DuckDB, allowing you to i
77
88
## Features
99
Currently supported Redis operations:
10-
- `redis_get(key, host, port, password)`: Retrieves a value from Redis for a given key
11-
- `redis_set(key, value, host, port, password)`: Sets a value in Redis for a given key
10+
- String operations: `GET`, `SET`
11+
- Hash operations: `HGET`, `HSET`
12+
- List operations: `LPUSH`, `LRANGE`
1213

13-
Features:
14-
- Connection pooling for improved performance
15-
- Redis authentication support
16-
- Thread-safe operations
17-
- Detailed error handling
1814

1915
## Installation
2016
```sql
2117
INSTALL redis FROM community;
2218
LOAD redis;
2319
```
2420

25-
## Usage Examples
26-
### Connecting with Authentication
27-
```sql
28-
-- Set a value with authentication
29-
SELECT redis_set('user:1', 'John Doe', 'localhost', '6379', 'mypassword') as result;
30-
31-
-- Get a value with authentication
32-
SELECT redis_get('user:1', 'localhost', '6379', 'mypassword') as user_name;
21+
## Usage
22+
### Setting up Redis Connection
23+
First, create a secret to store your Redis connection details:
3324

34-
-- For non-authenticated Redis servers, pass an empty string as password
35-
SELECT redis_get('user:1', 'localhost', '6379', '') as user_name;
36-
```
37-
38-
### Setting Values in Redis
3925
```sql
40-
-- Set a single value
41-
SELECT redis_set('user:1', 'John Doe', 'localhost', '6379') as result;
26+
-- Create a Redis connection secret
27+
CREATE SECRET IF NOT EXISTS redis (
28+
TYPE redis,
29+
PROVIDER config,
30+
host 'localhost',
31+
port '6379',
32+
password 'optional_password'
33+
);
34+
35+
-- Create a Redis cloud connection secret
36+
CREATE SECRET IF NOT EXISTS redis (
37+
TYPE redis,
38+
PROVIDER config,
39+
host 'redis-1234.ec2.redns.redis-cloud.com',
40+
port '16959',
41+
password 'xxxxxx'
42+
);
43+
```
44+
45+
### String Operations
46+
```sql
47+
-- Set a value
48+
SELECT redis_set('user:1', 'John Doe', 'redis') as result;
49+
50+
-- Get a value
51+
SELECT redis_get('user:1', 'redis') as user_name;
4252

4353
-- Set multiple values in a query
44-
INSERT INTO users (id, name, age)
45-
SELECT redis_set(
54+
INSERT INTO users (id, name)
55+
SELECT id, redis_set(
4656
'user:' || id::VARCHAR,
4757
name,
48-
'localhost',
49-
'6379'
58+
'my_redis'
5059
)
5160
FROM new_users;
5261
```
5362

54-
### Getting Values from Redis
63+
### Hash Operations
5564
```sql
56-
-- Get a single value
57-
SELECT redis_get('user:1', 'localhost', '6379') as user_name;
58-
59-
-- Get multiple values
60-
SELECT
61-
id,
62-
redis_get('user:' || id::VARCHAR, 'localhost', '6379') as user_data
63-
FROM user_ids;
65+
-- Set hash fields
66+
SELECT redis_hset('user:1', 'email', '[email protected]', 'redis');
67+
SELECT redis_hset('user:1', 'age', '30', 'redis');
68+
69+
-- Get hash field
70+
SELECT redis_hget('user:1', 'email', 'redis') as email;
71+
72+
-- Store user profile as hash
73+
WITH profile(id, field, value) AS (
74+
VALUES
75+
(1, 'name', 'John Doe'),
76+
(1, 'email', '[email protected]'),
77+
(1, 'age', '30')
78+
)
79+
SELECT redis_hset(
80+
'user:' || id::VARCHAR,
81+
field,
82+
value,
83+
'redis'
84+
)
85+
FROM profile;
6486
```
6587

66-
## Building from Source
67-
Follow the standard DuckDB extension build process:
88+
### List Operations
89+
```sql
90+
-- Push items to list
91+
SELECT redis_lpush('mylist', 'first_item', 'redis');
92+
SELECT redis_lpush('mylist', 'second_item', 'redis');
6893

69-
```sh
70-
# Install vcpkg dependencies
71-
./vcpkg/vcpkg install boost-asio
94+
-- Get range from list (returns comma-separated values)
95+
-- Get all items (0 to -1 means start to end)
96+
SELECT redis_lrange('mylist', 0, -1, 'redis') as items;
7297

73-
# Build the extension
74-
make
75-
```
98+
-- Get first 5 items
99+
SELECT redis_lrange('mylist', 0, 4, 'redis') as items;
76100

77-
## Dependencies
78-
- Boost.Asio (header-only, installed via vcpkg)
101+
-- Push multiple items
102+
WITH items(value) AS (
103+
VALUES ('item1'), ('item2'), ('item3')
104+
)
105+
SELECT redis_lpush('mylist', value, 'redis')
106+
FROM items;
107+
```
79108

80109
## Error Handling
81110
The extension functions will throw exceptions with descriptive error messages when:
111+
- Redis secret is not found or invalid
82112
- Unable to connect to Redis server
83113
- Network communication errors occur
84114
- Invalid Redis protocol responses are received
85115

86-
## Future Enhancements
87-
Planned features include:
88-
- Support for Redis authentication
89-
- Connection pooling for better performance
90-
- Additional Redis commands (HGET, HSET, LPUSH, etc.)
91-
- Table functions for scanning Redis keys
92-
- Batch operations using Redis pipelines
93-
- Connection timeout handling
94-
95-
# DuckDB Extension Template
96-
This repository contains a template for creating a DuckDB extension. The main goal of this template is to allow users to easily develop, test and distribute their own DuckDB extension. The main branch of the template is always based on the latest stable DuckDB allowing you to try out your extension right away.
116+
## Building from Source
117+
Follow the standard DuckDB extension build process:
97118

98-
## Getting started
99-
First step to getting started is to create your own repo from this template by clicking `Use this template`. Then clone your new repository using
100119
```sh
101-
git clone --recurse-submodules https://github.com/<you>/<your-new-extension-repo>.git
102-
```
103-
Note that `--recurse-submodules` will ensure DuckDB is pulled which is required to build the extension.
104-
105-
## Building
106-
### Managing dependencies
107-
DuckDB extensions uses VCPKG for dependency management. Enabling VCPKG is very simple: follow the [installation instructions](https://vcpkg.io/en/getting-started) or just run the following:
108-
```shell
109-
cd <your-working-dir-not-the-plugin-repo>
110-
git clone https://github.com/Microsoft/vcpkg.git
111-
sh ./vcpkg/scripts/bootstrap.sh -disableMetrics
112-
export VCPKG_TOOLCHAIN_PATH=`pwd`/vcpkg/scripts/buildsystems/vcpkg.cmake
113-
```
114-
Note: VCPKG is only required for extensions that want to rely on it for dependency management. If you want to develop an extension without dependencies, or want to do your own dependency management, just skip this step. Note that the example extension uses VCPKG to build with a dependency for instructive purposes, so when skipping this step the build may not work without removing the dependency.
120+
# Install vcpkg dependencies
121+
./vcpkg/vcpkg install boost-asio
115122

116-
### Build steps
117-
Now to build the extension, run:
118-
```sh
123+
# Build the extension
119124
make
120125
```
121-
The main binaries that will be built are:
122-
```sh
123-
./build/release/duckdb
124-
./build/release/test/unittest
125-
./build/release/extension/<extension_name>/<extension_name>.duckdb_extension
126-
```
127-
- `duckdb` is the binary for the duckdb shell with the extension code automatically loaded.
128-
- `unittest` is the test runner of duckdb. Again, the extension is already linked into the binary.
129-
- `<extension_name>.duckdb_extension` is the loadable binary as it would be distributed.
130-
131-
### Tips for speedy builds
132-
DuckDB extensions currently rely on DuckDB's build system to provide easy testing and distributing. This does however come at the downside of requiring the template to build DuckDB and its unittest binary every time you build your extension. To mitigate this, we highly recommend installing [ccache](https://ccache.dev/) and [ninja](https://ninja-build.org/). This will ensure you only need to build core DuckDB once and allows for rapid rebuilds.
133-
134-
To build using ninja and ccache ensure both are installed and run:
135-
136-
```sh
137-
GEN=ninja make
138-
```
139-
140-
## Running the extension
141-
To run the extension code, simply start the shell with `./build/release/duckdb`. This shell will have the extension pre-loaded.
142-
143-
Now we can use the features from the extension directly in DuckDB. The template contains a single scalar function `quack()` that takes a string arguments and returns a string:
144-
```
145-
D select quack('Jane') as result;
146-
┌───────────────┐
147-
│ result │
148-
│ varchar │
149-
├───────────────┤
150-
│ Quack Jane 🐥 │
151-
└───────────────┘
152-
```
153126

154-
## Running the tests
155-
Different tests can be created for DuckDB extensions. The primary way of testing DuckDB extensions should be the SQL tests in `./test/sql`. These SQL tests can be run using:
156-
```sh
157-
make test
158-
```
159-
160-
## Getting started with your own extension
161-
After creating a repository from this template, the first step is to name your extension. To rename the extension, run:
162-
```
163-
python3 ./scripts/bootstrap-template.py <extension_name_you_want>
164-
```
165-
Feel free to delete the script after this step.
166-
167-
Now you're good to go! After a (re)build, you should now be able to use your duckdb extension:
168-
```
169-
./build/release/duckdb
170-
D select <extension_name_you_chose>('Jane') as result;
171-
┌─────────────────────────────────────┐
172-
│ result │
173-
│ varchar │
174-
├─────────────────────────────────────┤
175-
│ <extension_name_you_chose> Jane 🐥 │
176-
└─────────────────────────────────────┘
177-
```
178-
179-
For inspiration/examples on how to extend DuckDB in a more meaningful way, check out the [test extensions](https://github.com/duckdb/duckdb/blob/main/test/extension),
180-
the [in-tree extensions](https://github.com/duckdb/duckdb/tree/main/extension), and the [out-of-tree extensions](https://github.com/duckdblabs).
181-
182-
## Distributing your extension
183-
To distribute your extension binaries, there are a few options.
184-
185-
### Community extensions
186-
The recommended way of distributing extensions is through the [community extensions repository](https://github.com/duckdb/community-extensions).
187-
This repository is designed specifically for extensions that are built using this extension template, meaning that as long as your extension can be
188-
built using the default CI in this template, submitting it to the community extensions is a very simple process. The process works similarly to popular
189-
package managers like homebrew and vcpkg, where a PR containing a descriptor file is submitted to the package manager repository. After the CI in the
190-
community extensions repository completes, the extension can be installed and loaded in DuckDB with:
191-
```SQL
192-
INSTALL <my_extension> FROM community;
193-
LOAD <my_extension>
194-
```
195-
For more information, see the [community extensions documentation](https://duckdb.org/community_extensions/documentation).
196-
197-
### Downloading artifacts from GitHub
198-
The default CI in this template will automatically upload the binaries for every push to the main branch as GitHub Actions artifacts. These
199-
can be downloaded manually and then loaded directly using:
200-
```SQL
201-
LOAD '/path/to/downloaded/extension.duckdb_extension';
202-
```
203-
Note that this will require starting DuckDB with the
204-
`allow_unsigned_extensions` option set to true. How to set this will depend on the client you're using. For the CLI it is done like:
205-
```shell
206-
duckdb -unsigned
207-
```
208-
209-
### Uploading to a custom repository
210-
If for some reason distributing through community extensions is not an option, extensions can also be uploaded to a custom extension repository.
211-
This will give some more control over where and how the extensions are distributed, but comes with the downside of requiring the `allow_unsigned_extensions`
212-
option to be set. For examples of how to configure a manual GitHub Actions deploy pipeline, check out the extension deploy script in https://github.com/duckdb/extension-ci-tools.
213-
Some examples of extensions that use this CI/CD workflow check out [spatial](https://github.com/duckdblabs/duckdb_spatial) or [aws](https://github.com/duckdb/duckdb_aws).
214-
215-
Extensions in custom repositories can be installed and loaded using:
216-
```SQL
217-
INSTALL <my_extension> FROM 'http://my-custom-repo'
218-
LOAD <my_extension>
219-
```
220-
221-
### Versioning of your extension
222-
Extension binaries will only work for the specific DuckDB version they were built for. The version of DuckDB that is targeted
223-
is set to the latest stable release for the main branch of the template so initially that is all you need. As new releases
224-
of DuckDB are published however, the extension repository will need to be updated. The template comes with a workflow set-up
225-
that will automatically build the binaries for all DuckDB target architectures that are available in the corresponding DuckDB
226-
version. This workflow is found in `.github/workflows/MainDistributionPipeline.yml`. It is up to the extension developer to keep
227-
this up to date with DuckDB. Note also that its possible to distribute binaries for multiple DuckDB versions in this workflow
228-
by simply duplicating the jobs.
229-
230-
## Setting up CLion
231-
232-
### Opening project
233-
Configuring CLion with the extension template requires a little work. Firstly, make sure that the DuckDB submodule is available.
234-
Then make sure to open `./duckdb/CMakeLists.txt` (so not the top level `CMakeLists.txt` file from this repo) as a project in CLion.
235-
Now to fix your project path go to `tools->CMake->Change Project Root`([docs](https://www.jetbrains.com/help/clion/change-project-root-directory.html)) to set the project root to the root dir of this repo.
236-
237-
### Debugging
238-
To set up debugging in CLion, there are two simple steps required. Firstly, in `CLion -> Settings / Preferences -> Build, Execution, Deploy -> CMake` you will need to add the desired builds (e.g. Debug, Release, RelDebug, etc). There's different ways to configure this, but the easiest is to leave all empty, except the `build path`, which needs to be set to `../build/{build type}`. Now on a clean repository you will first need to run `make {build type}` to initialize the CMake build directory. After running make, you will be able to (re)build from CLion by using the build target we just created. If you use the CLion editor, you can create a CLion CMake profiles matching the CMake variables that are described in the makefile, and then you don't need to invoke the Makefile.
127+
## Future Enhancements
128+
Planned features include:
129+
- Table functions for scanning Redis keys
130+
- Additional Redis commands (SADD, SMEMBERS, etc.)
131+
- Batch operations using Redis pipelines
132+
- Connection timeout handling
239133

240-
The second step is to configure the unittest runner as a run/debug configuration. To do this, go to `Run -> Edit Configurations` and click `+ -> Cmake Application`. The target and executable should be `unittest`. This will run all the DuckDB tests. To specify only running the extension specific tests, add `--test-dir ../../.. [sql]` to the `Program Arguments`. Note that it is recommended to use the `unittest` executable for testing/development within CLion. The actual DuckDB CLI currently does not reliably work as a run target in CLion.

duckdb

Submodule duckdb updated 1576 files

src/include/redis_extension.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "duckdb.hpp"
4+
#include "redis_secret.hpp"
45

56
namespace duckdb {
67

@@ -10,4 +11,4 @@ class RedisExtension : public Extension {
1011
std::string Name() override;
1112
};
1213

13-
} // namespace duckdb
14+
} // namespace duckdb

src/include/redis_secret.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include "duckdb/main/secret/secret.hpp"
4+
#include "duckdb/main/secret/secret_manager.hpp"
5+
#include "duckdb/main/extension_util.hpp"
6+
7+
namespace duckdb {
8+
9+
class CreateRedisSecretFunctions {
10+
public:
11+
static void Register(DatabaseInstance &instance);
12+
};
13+
14+
} // namespace duckdb

0 commit comments

Comments
 (0)