Skip to content

Commit 7c10f49

Browse files
authored
feat(duckdb): s3 config support (#6961)
1 parent fd9f408 commit 7c10f49

File tree

2 files changed

+76
-27
lines changed

2 files changed

+76
-27
lines changed

packages/cubejs-backend-shared/src/env.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,23 +1359,53 @@ const variables: Record<string, (...args: any) => any> = {
13591359
* duckdb *
13601360
***************************************************************** */
13611361

1362-
duckdbHttpFs: ({
1362+
duckdbMotherDuckToken: ({
13631363
dataSource
13641364
}: {
13651365
dataSource: string,
13661366
}) => (
13671367
process.env[
1368-
keyByDataSource('CUBEJS_DB_DUCKDB_HTTP_FS', dataSource)
1368+
keyByDataSource('CUBEJS_DB_DUCKDB_MOTHERDUCK_TOKEN', dataSource)
13691369
]
13701370
),
1371-
1372-
duckdbMotherDuckToken: ({
1371+
1372+
duckdbS3Region: ({
13731373
dataSource
13741374
}: {
13751375
dataSource: string,
13761376
}) => (
13771377
process.env[
1378-
keyByDataSource('CUBEJS_DB_DUCKDB_MOTHERDUCK_TOKEN', dataSource)
1378+
keyByDataSource('CUBEJS_DB_DUCKDB_S3_REGION', dataSource)
1379+
]
1380+
),
1381+
1382+
duckdbS3AccessKeyId: ({
1383+
dataSource
1384+
}: {
1385+
dataSource: string,
1386+
}) => (
1387+
process.env[
1388+
keyByDataSource('CUBEJS_DB_DUCKDB_S3_ACCESS_KEY_ID', dataSource)
1389+
]
1390+
),
1391+
1392+
duckdbS3SecretAccessKeyId: ({
1393+
dataSource
1394+
}: {
1395+
dataSource: string,
1396+
}) => (
1397+
process.env[
1398+
keyByDataSource('CUBEJS_DB_DUCKDB_S3_SECRET_ACCESS_KEY', dataSource)
1399+
]
1400+
),
1401+
1402+
duckdbS3Endpoint: ({
1403+
dataSource
1404+
}: {
1405+
dataSource: string,
1406+
}) => (
1407+
process.env[
1408+
keyByDataSource('CUBEJS_DB_DUCKDB_S3_ENDPOINT', dataSource)
13791409
]
13801410
),
13811411

packages/cubejs-duckdb-driver/src/DuckDBDriver.ts

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
StreamOptions,
55
QueryOptions, StreamTableData,
66
} from '@cubejs-backend/base-driver';
7-
import { assertDataSource, getEnv } from '@cubejs-backend/shared';
7+
import { getEnv } from '@cubejs-backend/shared';
88
import { promisify } from 'util';
99
import * as stream from 'stream';
1010
// eslint-disable-next-line import/no-extraneous-dependencies
@@ -15,28 +15,16 @@ import { HydrationStream, transformRow } from './HydrationStream';
1515

1616
export type DuckDBDriverConfiguration = {
1717
dataSource?: string,
18-
enableHttpFs?: boolean,
1918
initSql?: string,
2019
};
2120

2221
export class DuckDBDriver extends BaseDriver implements DriverInterface {
23-
protected readonly config: DuckDBDriverConfiguration;
24-
2522
protected initPromise: Promise<Database> | null = null;
2623

2724
public constructor(
28-
config: DuckDBDriverConfiguration = {},
25+
protected readonly config: DuckDBDriverConfiguration = {},
2926
) {
3027
super();
31-
32-
const dataSource =
33-
config.dataSource ||
34-
assertDataSource('default');
35-
36-
this.config = {
37-
enableHttpFs: getEnv('duckdbHttpFs', { dataSource }) || true,
38-
...config,
39-
};
4028
}
4129

4230
protected async initDatabase(): Promise<Database> {
@@ -45,16 +33,47 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
4533
const db = new Database(token ? `md:?motherduck_token=${token}` : ':memory:');
4634
const conn = db.connect();
4735

48-
if (this.config.enableHttpFs) {
49-
try {
50-
await this.handleQuery(conn, 'INSTALL httpfs', []);
51-
} catch (e) {
52-
if (this.logger) {
53-
console.error('DuckDB - error on httpfs installation', {
54-
e
55-
});
36+
const s3InitQuries = [
37+
{
38+
key: 's3_region',
39+
value: getEnv('duckdbS3Region', this.config),
40+
},
41+
{
42+
key: 's3_endpoint',
43+
value: getEnv('duckdbS3Endpoint', this.config),
44+
},
45+
{
46+
key: 's3_access_key_id',
47+
value: getEnv('duckdbS3AccessKeyId', this.config),
48+
},
49+
{
50+
key: 's3_secret_access_key',
51+
value: getEnv('duckdbS3SecretAccessKeyId', this.config),
52+
},
53+
];
54+
55+
try {
56+
await this.handleQuery(conn, 'INSTALL httpfs', []);
57+
} catch (e) {
58+
if (this.logger) {
59+
console.error('DuckDB - error on httpfs installation', {
60+
e
61+
});
62+
}
63+
}
64+
65+
try {
66+
for (const { key, value } of s3InitQuries) {
67+
if (value) {
68+
await this.handleQuery(conn, `SET ${key}='${value}'`, []);
5669
}
5770
}
71+
} catch (e) {
72+
if (this.logger) {
73+
console.error('DuckDB - error on s3 configuration', {
74+
e
75+
});
76+
}
5877
}
5978

6079
if (this.config.initSql) {

0 commit comments

Comments
 (0)