Skip to content

Commit

Permalink
feat(materialize-driver): Add cluster option to Materialize driver (#…
Browse files Browse the repository at this point in the history
…7773) Thanks @bobbyiliev!

* Add cluster option to Materialize driver

* Fix lint error

* Set connection application_name

* Update materialized docker image

* Disable SSL for smoke tests

* Change the name of the MZ cluster env var

* Add link to Materialize Slack

* Fix failing tests due to SSL error
  • Loading branch information
bobbyiliev committed Feb 27, 2024
1 parent db37fc3 commit 917004e
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 17 deletions.
20 changes: 11 additions & 9 deletions docs/pages/product/configuration/data-sources/materialize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ CUBEJS_DB_PORT=6875
CUBEJS_DB_NAME=materialize
CUBEJS_DB_USER=materialize
CUBEJS_DB_PASS=materialize
CUBEJS_DB_MATERIALIZE_CLUSTER=quickstart
```

## Environment Variables

| Environment Variable | Description | Possible Values | Required |
| -------------------- | ----------------------------------------------------------------------------------- | ------------------------- | :------: |
| `CUBEJS_DB_HOST` | The host URL for a database | A valid database host URL ||
| `CUBEJS_DB_PORT` | The port for the database connection | A valid port number ||
| `CUBEJS_DB_NAME` | The name of the database to connect to | A valid database name ||
| `CUBEJS_DB_USER` | The username used to connect to the database | A valid database username ||
| `CUBEJS_DB_PASS` | The password used to connect to the database | A valid database password ||
| `CUBEJS_CONCURRENCY` | The number of concurrent connections each queue has to the database. Default is `2` | A valid number ||
| `CUBEJS_DB_MAX_POOL` | The maximum number of concurrent database connections to pool. Default is `8` | A valid number ||
| Environment Variable | Description | Possible Values | Required |
| -------------------------------- | ----------------------------------------------------------------------------------- | ------------------------- | :------: |
| `CUBEJS_DB_HOST` | The host URL for a database | A valid database host URL ||
| `CUBEJS_DB_PORT` | The port for the database connection | A valid port number ||
| `CUBEJS_DB_NAME` | The name of the database to connect to | A valid database name ||
| `CUBEJS_DB_USER` | The username used to connect to the database | A valid database username ||
| `CUBEJS_DB_PASS` | The password used to connect to the database | A valid database password ||
| `CUBEJS_CONCURRENCY` | The number of concurrent connections each queue has to the database. Default is `2` | A valid number ||
| `CUBEJS_DB_MAX_POOL` | The maximum number of concurrent database connections to pool. Default is `8` | A valid number ||
| `CUBEJS_DB_MATERIALIZE_CLUSTER` | The name of the Materialize cluster to connect to | A valid cluster name ||

## SSL

Expand Down
17 changes: 17 additions & 0 deletions packages/cubejs-backend-shared/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,23 @@ const variables: Record<string, (...args: any) => any> = {
]
),

/** ****************************************************************
* Materialize Driver *
***************************************************************** */

/**
* Materialize cluster.
*/
materializeCluster: ({
dataSource
}: {
dataSource: string,
}) => (
process.env[
keyByDataSource('CUBEJS_DB_MATERIALIZE_CLUSTER', dataSource)
]
),

/** ****************************************************************
* Snowflake Driver *
***************************************************************** */
Expand Down
2 changes: 1 addition & 1 deletion packages/cubejs-materialize-driver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pure Javascript Materialize driver. Based on pg driver for PostgreSQL.

## Support

This driver has been contributed by [Joaquin Colacci](https://github.com/joacoc). This package is **community supported** and should be used at your own risk.
This driver has been contributed by [Joaquin Colacci](https://github.com/joacoc) and is currently maintained by [Materialize](https://www.materialize.com/s/chat). This package is **community supported** and should be used at your own risk.

While the Cube Dev team is happy to review and accept future community contributions, we don't have active plans for further development. This includes bug fixes unless they affect different parts of Cube.js. **We're looking for maintainers for this package.** If you'd like to become a maintainer, please contact us in Cube.js Slack.

Expand Down
32 changes: 32 additions & 0 deletions packages/cubejs-materialize-driver/src/MaterializeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,35 @@ export class MaterializeDriver extends PostgresDriver {
* request before determining it as not valid. Default - 10000 ms.
*/
testConnectionTimeout?: number,

/**
* Optional cluster name to set for the connection.
*/
cluster?: string,

/**
* SSL is enabled by default. Set to false to disable.
*/
ssl?: boolean | { rejectUnauthorized: boolean },

/**
* Application name to set for the connection.
*/
application_name?: string,
} = {},
) {
// Enable SSL by default if not set explicitly to false
const sslEnv = process.env.CUBEJS_DB_SSL;
if (sslEnv === 'false') {
options.ssl = false;
} else if (sslEnv === 'true') {
options.ssl = { rejectUnauthorized: true };
} else if (options.ssl === undefined) {
options.ssl = true;
}
// Set application name to 'cubejs-materialize-driver' by default
options.application_name = options.application_name || 'cubejs-materialize-driver';

super(options);
}

Expand All @@ -69,6 +96,11 @@ export class MaterializeDriver extends PostgresDriver {
) {
await conn.query(`SET TIME ZONE '${this.config.storeTimezone || 'UTC'}'`);
// Support for statement_timeout is still pending. https://github.com/MaterializeInc/materialize/issues/10390

// Set cluster to the CUBEJS_DB_MATERIALIZE_CLUSTER env variable if it exists
if (process.env.CUBEJS_DB_MATERIALIZE_CLUSTER) {
await conn.query(`SET CLUSTER TO ${process.env.CUBEJS_DB_MATERIALIZE_CLUSTER}`);
}
}

protected async loadUserDefinedTypes(): Promise<void> {
Expand Down
15 changes: 13 additions & 2 deletions packages/cubejs-materialize-driver/test/MaterializeDriver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe('MaterializeDriver', () => {
user: 'materialize',
password: 'materialize',
database: 'materialize',
cluster: 'quickstart',
ssl: false,
});
await driver.query('CREATE SCHEMA IF NOT EXISTS test;', []);
});
Expand Down Expand Up @@ -71,8 +73,8 @@ describe('MaterializeDriver', () => {
test('schema detection', async () => {
await Promise.all([
driver.query('CREATE TABLE A (a INT, b BIGINT, c TEXT, d DOUBLE, e FLOAT);', []),
driver.query('CREATE VIEW V AS SELECT * FROM mz_views;', []),
driver.query('CREATE MATERIALIZED VIEW MV AS SELECT * FROM mz_views;', []),
driver.query('CREATE VIEW V AS SELECT * FROM A;', []),
driver.query('CREATE MATERIALIZED VIEW MV AS SELECT * FROM A;', []),
]);

const tablesSchemaData = await driver.tablesSchema();
Expand Down Expand Up @@ -149,4 +151,13 @@ describe('MaterializeDriver', () => {
);
}
});

test('cluster', async () => {
const data = await driver.query(`SHOW CLUSTER;`, []);
expect(data).toEqual([
{
'cluster': 'quickstart',
}]);
});

});
10 changes: 9 additions & 1 deletion packages/cubejs-playground/src/shared/env-vars-db-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const envVarsDbMap = [
{ title: 'Hive/SparkSQL', driver: 'hive', logo: logoHive },
{ title: 'Oracle', driver: 'oracle', logo: logoOracle },
{ title: 'QuestDB', driver: 'questdb', logo: logoQuestdb },
{ title: 'Materialize', driver: 'materialize', logo: logoMaterialize },
{ title: 'Crate', driver: 'crate', logo: logoCrate },
],
settings: [...BASE_SERVER, DB_NAME, ...BASE_CRED],
Expand Down Expand Up @@ -119,6 +118,15 @@ Upload a service account JSON keyfile to connect to BigQuery.<br/>Alternatively,
// { env: 'CUBEJS_DB_SSL_PASSPHRASE' }
],
},
{
databases: [{ title: 'Materialize', driver: 'materialize', logo: logoMaterialize },],
settings: [
...BASE_SERVER,
...BASE_CRED,
DB_NAME,
{ env: 'CUBEJS_DB_MATERIALIZE_CLUSTER', title: 'Cluster' },
],
},
{
databases: [
{ title: 'Snowflake', driver: 'snowflake', logo: logoSnowflake },
Expand Down
5 changes: 1 addition & 4 deletions packages/cubejs-testing-shared/src/db/materialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ type MaterializeStartOptions = DBRunnerContainerOptions & {

export class MaterializeDBRunner extends DbRunnerAbstract {
public static startContainer(options: MaterializeStartOptions) {
/**
* Version v0.26.0 is latest Materialize release with Long Term Support
*/
const version = process.env.TEST_MZSQL_VERSION || options.version || 'v0.26.0';
const version = process.env.TEST_MZSQL_VERSION || options.version || 'v0.88.0';

const container = new GenericContainer(`materialize/materialized:${version}`)
.withExposedPorts(6875)
Expand Down
1 change: 1 addition & 0 deletions packages/cubejs-testing/test/smoke-materialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('materialize', () => {
CUBEJS_DB_NAME: 'materialize',
CUBEJS_DB_USER: 'materialize',
CUBEJS_DB_PASS: 'materialize',
CUBEJS_DB_SSL: 'false',

...DEFAULT_CONFIG,
},
Expand Down

0 comments on commit 917004e

Please sign in to comment.