Skip to content

Commit 0bc9b46

Browse files
committed
chore(base-driver,redshift-driver,bigquery-driver,materialize-driver): Speed up table schema retrospection
Speed up by one time inplace sorting in stead of a few and with new objects creation
1 parent 93a4dcb commit 0bc9b46

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import {
5353
TableMemoryData,
5454
PrimaryKeysQueryResult,
5555
ForeignKeysQueryResult,
56-
DatabaseStructure,
56+
DatabaseStructure, InformationSchemaColumn,
5757
} from './driver.interface';
5858

5959
/**
@@ -427,28 +427,43 @@ export abstract class BaseDriver implements DriverInterface {
427427
return false;
428428
}
429429

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

434-
columns.push({
439+
result[i.table_schema][i.table_name].push({
435440
name: i.column_name,
436441
type: i.data_type,
437442
attributes: i.key_type ? ['primaryKey'] : []
438443
});
439444

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

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

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

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

454469
// 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)