Skip to content

Commit fc8a195

Browse files
authored
chore(base-driver,redshift-driver,bigquery-driver,materialize-driver): Speed up table schema retrospection (#9751)
Speed up by one time inplace sorting in stead of a few and with new objects creation
1 parent ea2a59f commit fc8a195

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

packages/cubejs-base-driver/src/BaseDriver.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
PrimaryKeysQueryResult,
5555
ForeignKeysQueryResult,
5656
DatabaseStructure,
57+
InformationSchemaColumn,
5758
} from './driver.interface';
5859

5960
/**
@@ -427,28 +428,43 @@ export abstract class BaseDriver implements DriverInterface {
427428
return false;
428429
}
429430

430-
protected informationColumnsSchemaReducer(result: any, i: any): DatabaseStructure {
431-
let schema = (result[i.table_schema] || {});
432-
const columns = (schema[i.table_name] || []);
431+
protected informationColumnsSchemaReducer(result: DatabaseStructure, i: InformationSchemaColumn): DatabaseStructure {
432+
if (!result[i.table_schema]) {
433+
result[i.table_schema] = {};
434+
}
435+
436+
if (!result[i.table_schema][i.table_name]) {
437+
result[i.table_schema][i.table_name] = [];
438+
}
433439

434-
columns.push({
440+
result[i.table_schema][i.table_name].push({
435441
name: i.column_name,
436442
type: i.data_type,
437443
attributes: i.key_type ? ['primaryKey'] : []
438444
});
439445

440-
columns.sort();
441-
schema[i.table_name] = columns;
442-
schema = sortByKeys(schema);
443-
result[i.table_schema] = schema;
446+
return result;
447+
}
444448

445-
return sortByKeys(result);
449+
protected informationColumnsSchemaSorter(data: InformationSchemaColumn[]) {
450+
return data
451+
.map((i) => ({
452+
...i,
453+
sortedKeyServiceField: `${i.table_schema}.${i.table_name}.${i.column_name}`,
454+
}))
455+
.sort((a, b) => a.sortedKeyServiceField.localeCompare(b.sortedKeyServiceField));
446456
}
447457

448-
public tablesSchema(): Promise<DatabaseStructure> {
458+
public async tablesSchema(): Promise<DatabaseStructure> {
449459
const query = this.informationSchemaQuery();
460+
const data: InformationSchemaColumn[] = await this.query(query, []);
461+
462+
if (!data.length) {
463+
return {};
464+
}
450465

451-
return this.query(query, []).then(data => data.reduce<DatabaseStructure>(this.informationColumnsSchemaReducer, {}));
466+
const sortedData = this.informationColumnsSchemaSorter(data);
467+
return sortedData.reduce<DatabaseStructure>(this.informationColumnsSchemaReducer, {});
452468
}
453469

454470
// Extended version of tablesSchema containing primary and foreign keys

packages/cubejs-base-driver/src/driver.interface.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,18 @@ export type ForeignKeysQueryResult = {
212212
target_column: string
213213
};
214214

215+
export type InformationSchemaColumn = {
216+
// eslint-disable-next-line camelcase
217+
table_schema: string;
218+
// eslint-disable-next-line camelcase
219+
table_name: string;
220+
// eslint-disable-next-line camelcase
221+
column_name: string;
222+
// eslint-disable-next-line camelcase
223+
data_type: string;
224+
[key: string]: string
225+
};
226+
215227
export type TableKeysFilter = {
216228
tableSchema: string,
217229
tableName: string[]

packages/cubejs-bigquery-driver/src/BigQueryDriver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export class BigQueryDriver extends BaseDriver implements DriverInterface {
202202

203203
if (result.length) {
204204
return R.reduce(
205-
this.informationColumnsSchemaReducer, {}, result[0]
205+
this.informationColumnsSchemaReducer, {}, this.informationColumnsSchemaSorter(result[0])
206206
);
207207
}
208208

packages/cubejs-materialize-driver/src/MaterializeDriver.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
DatabaseStructure,
1111
DownloadTableMemoryData,
1212
IndexesSQL,
13+
InformationSchemaColumn,
1314
StreamOptions,
1415
StreamTableDataWithTypes,
1516
TableStructure
@@ -173,11 +174,13 @@ export class MaterializeDriver extends PostgresDriver {
173174
return version.split(' ')[0];
174175
}
175176

176-
public async tablesSchema(): Promise<DatabaseStructure> {
177+
public override async tablesSchema(): Promise<DatabaseStructure> {
177178
const version = await this.getMaterializeVersion();
178179
const query = this.informationSchemaQueryWithFilter(version);
180+
const data: InformationSchemaColumn[] = await this.query(query, []);
181+
const sortedData = this.informationColumnsSchemaSorter(data);
179182

180-
return this.query(query, []).then(data => data.reduce<DatabaseStructure>(this.informationColumnsSchemaReducer, {}));
183+
return sortedData.reduce<DatabaseStructure>(this.informationColumnsSchemaReducer, {});
181184
}
182185

183186
protected async* asyncFetcher<R extends unknown>(conn: PoolClient, cursorId: string): AsyncGenerator<R> {

packages/cubejs-redshift-driver/src/RedshiftDriver.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
DatabaseStructure,
1111
DownloadTableCSVData,
1212
DriverCapabilities,
13+
InformationSchemaColumn,
1314
QueryColumnsResult,
1415
QuerySchemasResult,
1516
QueryTablesResult,
@@ -137,7 +138,9 @@ export class RedshiftDriver extends PostgresDriver<RedshiftDriverConfiguration>
137138
*/
138139
public override async tablesSchema(): Promise<DatabaseStructure> {
139140
const query = this.informationSchemaQuery();
140-
const tablesSchema = await this.query(query, []).then(data => data.reduce<DatabaseStructure>(this.informationColumnsSchemaReducer, {}));
141+
const data: InformationSchemaColumn[] = await this.query(query, []);
142+
const tablesSchema = this.informationColumnsSchemaSorter(data)
143+
.reduce<DatabaseStructure>(this.informationColumnsSchemaReducer, {});
141144

142145
const allSchemas = await this.getSchemas();
143146
const externalSchemas = allSchemas.filter(s => !tablesSchema[s.schema_name]).map(s => s.schema_name);

0 commit comments

Comments
 (0)