Skip to content

Commit 32c603d

Browse files
authored
feat(server-core, query-orchestrator): sql-runner - supp… (#6142)
1 parent 6a4f763 commit 32c603d

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

packages/cubejs-cubestore-driver/src/CubeStoreDriver.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ export class CubeStoreDriver extends BaseDriver implements DriverInterface {
8181
}
8282

8383
public informationSchemaQuery() {
84-
return `${super.informationSchemaQuery()} AND columns.table_schema = '${this.config.database}'`;
84+
return `
85+
SELECT columns.column_name as ${this.quoteIdentifier('column_name')},
86+
columns.table_name as ${this.quoteIdentifier('table_name')},
87+
columns.table_schema as ${this.quoteIdentifier('table_schema')},
88+
columns.data_type as ${this.quoteIdentifier('data_type')}
89+
FROM information_schema.columns as columns
90+
WHERE columns.table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys', 'INFORMATION_SCHEMA')
91+
AND columns.table_schema = '${this.config.database}'
92+
`;
8593
}
8694

8795
public createTableSqlWithOptions(tableName, columns, options: CreateTableOptions) {

packages/cubejs-query-orchestrator/src/orchestrator/QueryCache.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ export class QueryCache {
428428
useCsvQuery,
429429
persistent,
430430
aliasNameToMember,
431+
tablesSchema,
431432
}: {
432433
cacheKey: CacheKey,
433434
dataSource: string,
@@ -438,6 +439,7 @@ export class QueryCache {
438439
useCsvQuery?: boolean,
439440
persistent?: boolean,
440441
aliasNameToMember?: { [alias: string]: string },
442+
tablesSchema?: boolean,
441443
}
442444
) {
443445
const queue = external
@@ -451,6 +453,7 @@ export class QueryCache {
451453
requestId,
452454
inlineTables,
453455
useCsvQuery,
456+
tablesSchema,
454457
};
455458

456459
const opt = {
@@ -530,6 +533,10 @@ export class QueryCache {
530533
`SQL_QUERY_EXT_${this.redisPrefix}`,
531534
this.options.externalDriverFactory,
532535
(client, q) => {
536+
if (q.tablesSchema) {
537+
return client.tablesSchema();
538+
}
539+
533540
this.logger('Executing SQL', {
534541
...q
535542
});
@@ -972,9 +979,4 @@ export class QueryCache {
972979
public async testConnection() {
973980
return this.cacheDriver.testConnection();
974981
}
975-
976-
public async fetchSchema(dataSource: string) {
977-
const queue = await this.getQueue(dataSource);
978-
return queue.executeQueryInQueue('query', [`Fetch schema for ${dataSource}`, []], { tablesSchema: true });
979-
}
980982
}

packages/cubejs-query-orchestrator/src/orchestrator/QueryOrchestrator.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,12 @@ export class QueryOrchestrator {
482482
return this.preAggregations.updateRefreshEndReached();
483483
}
484484

485-
public async fetchSchema(dataSource: string) {
486-
return this.queryCache.fetchSchema(dataSource);
485+
public async fetchSchema(dataSource?: string, external?: boolean) {
486+
return this.queryCache.queryWithRetryAndRelease('', [], {
487+
cacheKey: [`Fetch schema for ${dataSource}`, []],
488+
dataSource,
489+
external,
490+
tablesSchema: true
491+
});
487492
}
488493
}

packages/cubejs-query-orchestrator/test/unit/QueryOrchestrator.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ class MockDriver {
128128
}
129129

130130
class ExternalMockDriver extends MockDriver {
131-
constructor() {
132-
super();
131+
constructor({ schemaData } = {}) {
132+
super({ schemaData });
133133
this.indexes = [];
134134
this.csvFiles = [];
135135
}
@@ -223,7 +223,7 @@ describe('QueryOrchestrator', () => {
223223
const csvMockDriverLocal = new MockDriver({ csvImport: 'true' });
224224
const mockDriverUnloadWithoutTempTableSupportLocal = new MockDriverUnloadWithoutTempTableSupport();
225225
const streamingSourceMockDriverLocal = new StreamingSourceMockDriver();
226-
const externalMockDriverLocal = new ExternalMockDriver();
226+
const externalMockDriverLocal = new ExternalMockDriver({ schemaData });
227227

228228
const redisPrefix = `ORCHESTRATOR_TEST_${testCount++}`;
229229
const driverFactory = (dataSource) => {
@@ -1679,4 +1679,9 @@ describe('QueryOrchestrator', () => {
16791679
expect(data['Foo.query']).toMatch(/orders_d/);
16801680
}));
16811681
});
1682+
1683+
test('fetch table schema from external db', async () => {
1684+
const schema = await queryOrchestrator.fetchSchema(undefined, true);
1685+
expect(schema).toEqual(schemaData);
1686+
});
16821687
});

0 commit comments

Comments
 (0)