Skip to content

Commit

Permalink
fix(driver-materialize): filter non-materialized objects from schema (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
joacoc committed May 27, 2022
1 parent 8d05a9b commit a32a9a9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/cubejs-materialize-driver/src/MaterializeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ export class MaterializeDriver extends PostgresDriver {
return BaseDriver.prototype.uploadTableWithIndexes.bind(this)(table, columns, tableData, indexesSql, [], null);
}

/**
* Materialize quereable schema
* Returns materialized sources, materialized views, and tables
* @returns {string} schemaQuery
*/
public informationSchemaQuery(): string {
const materializationFilter = `
table_name IN (
SELECT name
FROM mz_catalog.mz_sources
WHERE mz_internal.mz_is_materialized(id)
UNION
SELECT name
FROM mz_catalog.mz_views
WHERE mz_internal.mz_is_materialized(id)
UNION
SELECT t.name
FROM mz_catalog.mz_tables t
)`;

return `${super.informationSchemaQuery()} AND ${materializationFilter}`;
}

protected async* asyncFetcher<R extends unknown>(conn: PoolClient, cursorId: string): AsyncGenerator<R> {
const timeout = `${this.config.executionTimeout ? <number>(this.config.executionTimeout) * 1000 : 600000} milliseconds`;
const queryParams = {
Expand Down
22 changes: 22 additions & 0 deletions packages/cubejs-materialize-driver/test/MaterializeDriver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ 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;', []),
]);

const tablesSchemaData = await driver.tablesSchema();
const { public: publicSchema } = tablesSchemaData;
const { a, v, mv } = publicSchema;

expect(a).toEqual([
{ name: 'c', type: 'text', attributes: [] },
{ name: 'b', type: 'bigint', attributes: [] },
{ name: 'a', type: 'integer', attributes: [] },
{ name: 'd', type: 'double precision', attributes: [] },
{ name: 'e', type: 'double precision', attributes: [] }
]);
expect(mv).toBeDefined();
expect(v).toBeUndefined();
});

test('stream', async () => {
await driver.uploadTable(
'test.streaming_test',
Expand Down

0 comments on commit a32a9a9

Please sign in to comment.