Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/pages/product/configuration/data-sources/duckdb.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ deployment][ref-demo-deployment] in Cube Cloud.
| `CUBEJS_DB_DUCKDB_S3_URL_STYLE` | To choose the S3 URL style(vhost or path) | 'vhost' or 'path' | ❌ | ❌ |
| `CUBEJS_DB_DUCKDB_S3_SESSION_TOKEN` | The token for the S3 session | A valid Session Token | ❌ | ✅ |
| `CUBEJS_DB_DUCKDB_EXTENSIONS` | A comma-separated list of DuckDB extensions to install and load | A comma-separated list of DuckDB extensions | ❌ | ✅ |
| `CUBEJS_DB_DUCKDB_COMMUNITY_EXTENSIONS` | A comma-separated list of DuckDB community extensions to install and load | A comma-separated list of DuckDB community extensions | ❌ | ✅ |
| `CUBEJS_CONCURRENCY` | The number of [concurrent queries][ref-data-source-concurrency] to the data source | A valid number | ❌ |

[ref-data-source-concurrency]: /product/configuration/concurrency#data-source-concurrency
Expand Down Expand Up @@ -133,4 +134,4 @@ connections are made over HTTPS.
/product/caching/using-pre-aggregations#pre-aggregation-build-strategies
[ref-schema-ref-types-formats-countdistinctapprox]: /reference/data-model/types-and-formats#count_distinct_approx
[self-preaggs-batching]: #batching
[ref-demo-deployment]: /product/deployment/cloud/deployments#demo-deployments
[ref-demo-deployment]: /product/deployment/cloud/deployments#demo-deployments
8 changes: 8 additions & 0 deletions docs/pages/reference/configuration/environment-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ A comma-separated list of DuckDB extensions to install and load.
| ------------------------------------------- | ---------------------- | --------------------- |
| A comma-separated list of DuckDB extensions | N/A | N/A |

## `CUBEJS_DB_DUCKDB_COMMUNITY_EXTENSIONS`

A comma-separated list of DuckDB community extensions to install and load.

| Possible Values | Default in Development | Default in Production |
| ----------------------------------------------------- | ---------------------- | --------------------- |
| A comma-separated list of DuckDB community extensions | N/A | N/A |

## `CUBEJS_DB_ELASTIC_APIKEY_ID`

The [ID of the API key from elastic.co][elastic-docs-api-keys]. Required when
Expand Down
13 changes: 13 additions & 0 deletions packages/cubejs-backend-shared/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,19 @@
}
return [];
},
duckdbCommunityExtensions: ({

Check warning on line 1776 in packages/cubejs-backend-shared/src/env.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1776 was not covered by tests
dataSource
}: {
dataSource: string,
}) => {
const extensions = process.env[

Check warning on line 1781 in packages/cubejs-backend-shared/src/env.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1781 was not covered by tests
keyByDataSource('CUBEJS_DB_DUCKDB_COMMUNITY_EXTENSIONS', dataSource)
];
if (extensions) {
return extensions.split(',').map(e => e.trim());

Check warning on line 1785 in packages/cubejs-backend-shared/src/env.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1785 was not covered by tests
}
return [];

Check warning on line 1787 in packages/cubejs-backend-shared/src/env.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1787 was not covered by tests
},
/** ***************************************************************
* Presto Driver *
**************************************************************** */
Expand Down
69 changes: 38 additions & 31 deletions packages/cubejs-duckdb-driver/src/DuckDBDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,38 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
return super.toGenericType(columnType.toLowerCase());
}

private async installExtensions(extensions: string[], execAsync: (sql: string, ...params: any[]) => Promise<void>, repository: string = ''): Promise<void> {
repository = repository ? ` FROM ${repository}` : '';
for (const extension of extensions) {
try {
await execAsync(`INSTALL ${extension}${repository}`);
} catch (e) {
if (this.logger) {
console.error(`DuckDB - error on installing ${extension}`, { e });
}
// DuckDB will lose connection_ref on connection on error, this will lead to broken connection object
throw e;
}
}
}

private async loadExtensions(extensions: string[], execAsync: (sql: string, ...params: any[]) => Promise<void>): Promise<void> {
for (const extension of extensions) {
try {
await execAsync(`LOAD ${extension}`);
} catch (e) {
if (this.logger) {
console.error(`DuckDB - error on loading ${extension}`, { e });
}
// DuckDB will lose connection_ref on connection on error, this will lead to broken connection object
throw e;
}
}
}

protected async init(): Promise<InitPromise> {
const token = this.config.motherDuckToken || getEnv('duckdbMotherDuckToken', this.config);
const dbPath = this.config.databasePath || getEnv('duckdbDatabasePath', this.config);

// Determine the database URL based on the provided db_path or token
let dbUrl: string;
if (dbPath) {
Expand Down Expand Up @@ -121,7 +149,7 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
value: getEnv('duckdbS3SessionToken', this.config),
}
];

for (const { key, value } of configuration) {
if (value) {
try {
Expand All @@ -137,34 +165,13 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
}

// Install & load extensions if configured in env variable.
const extensions = getEnv('duckdbExtensions', this.config);
for (const extension of extensions) {
try {
await execAsync(`INSTALL ${extension}`);
} catch (e) {
if (this.logger) {
console.error(`DuckDB - error on installing ${extension}`, {
e
});
}

// DuckDB will lose connection_ref on connection on error, this will lead to broken connection object
throw e;
}

try {
await execAsync(`LOAD ${extension}`);
} catch (e) {
if (this.logger) {
console.error(`DuckDB - error on loading ${extension}`, {
e
});
}

// DuckDB will lose connection_ref on connection on error, this will lead to broken connection object
throw e;
}
}
const officialExtensions = getEnv('duckdbExtensions', this.config);
await this.installExtensions(officialExtensions, execAsync);
await this.loadExtensions(officialExtensions, execAsync);
const communityExtensions = getEnv('duckdbCommunityExtensions', this.config);
// @see https://duckdb.org/community_extensions/
await this.installExtensions(communityExtensions, execAsync, 'community');
await this.loadExtensions(communityExtensions, execAsync);

if (this.config.initSql) {
try {
Expand All @@ -177,7 +184,7 @@ export class DuckDBDriver extends BaseDriver implements DriverInterface {
}
}
}

return {
defaultConnection,
db
Expand Down