Skip to content

Commit 0a3d646

Browse files
committed
Revert "feat: strategy without creating temp table (cube-js#5299)"
This reverts commit 8e8f500.
1 parent 9121a96 commit 0a3d646

File tree

6 files changed

+113
-298
lines changed

6 files changed

+113
-298
lines changed

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,7 @@ import fs from 'fs';
33
import { getEnv, isFilePath, isSslKey, isSslCert } from '@cubejs-backend/shared';
44

55
import { cancelCombinator } from './utils';
6-
import {
7-
CreateTableIndex,
8-
DownloadQueryResultsOptions,
9-
DownloadQueryResultsResult,
10-
DownloadTableCSVData,
11-
DownloadTableData,
12-
DownloadTableMemoryData,
13-
DriverInterface,
14-
ExternalDriverCompatibilities,
15-
IndexesSQL,
16-
isDownloadTableMemoryData,
17-
QueryOptions,
18-
Row,
19-
TableColumn,
20-
TableColumnQueryResult,
21-
TableQueryResult,
22-
TableStructure,
23-
DriverCapabilities
24-
} from './driver.interface';
6+
import { CreateTableIndex, DownloadQueryResultsOptions, DownloadQueryResultsResult, DownloadTableCSVData, DownloadTableData, DownloadTableMemoryData, DriverInterface, ExternalDriverCompatibilities, IndexesSQL, isDownloadTableMemoryData, QueryOptions, Row, Rows, TableColumn, TableColumnQueryResult, TableQueryResult, TableStructure } from './driver.interface';
257

268
const sortByKeys = (unordered) => {
279
const ordered = {};
@@ -383,7 +365,7 @@ export abstract class BaseDriver implements DriverInterface {
383365
// override, if it's needed
384366
}
385367

386-
public capabilities(): DriverCapabilities {
368+
public capabilities(): ExternalDriverCompatibilities {
387369
return {};
388370
}
389371

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ export interface ExternalDriverCompatibilities {
9797
csvImport?: true,
9898
streamImport?: true,
9999
}
100-
101-
export interface DriverCapabilities extends ExternalDriverCompatibilities {
102-
unloadWithoutTempTable?: true,
103-
}
104-
105100
export type StreamOptions = {
106101
highWaterMark: number;
107102
};
@@ -122,14 +117,8 @@ export type CreateTableIndex = {
122117
columns: string[]
123118
};
124119

125-
type UnloadQuery = {
126-
sql: string,
127-
params: unknown[]
128-
};
129-
130120
export type UnloadOptions = {
131121
maxFileSize: number,
132-
query?: UnloadQuery;
133122
};
134123

135124
export type QueryOptions = {
@@ -170,5 +159,5 @@ export interface DriverInterface {
170159
// Shutdown the driver
171160
release(): Promise<void>
172161

173-
capabilities(): DriverCapabilities;
162+
capabilities(): ExternalDriverCompatibilities;
174163
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import {
88
BaseDriver,
99
DownloadTableCSVData,
1010
DownloadTableMemoryData, DriverInterface, IndexesSQL, CreateTableIndex,
11-
StreamTableData,
12-
DriverCapabilities,
13-
StreamingSourceTableData,
14-
QueryOptions,
11+
StreamTableData, ExternalDriverCompatibilities,
12+
StreamingSourceTableData, QueryOptions,
1513
} from '@cubejs-backend/base-driver';
1614
import { getEnv } from '@cubejs-backend/shared';
1715
import { format as formatSql } from 'sqlstring';
@@ -366,7 +364,7 @@ export class CubeStoreDriver extends BaseDriver implements DriverInterface {
366364
return CubeStoreQuery;
367365
}
368366

369-
public capabilities(): DriverCapabilities {
367+
public capabilities(): ExternalDriverCompatibilities {
370368
return {
371369
csvImport: true,
372370
streamImport: true,

packages/cubejs-databricks-jdbc-driver/src/DatabricksDriver.ts

Lines changed: 47 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
SASProtocol,
1111
generateBlobSASQueryParameters,
1212
} from '@azure/storage-blob';
13-
import { DriverCapabilities, UnloadOptions, } from '@cubejs-backend/base-driver';
13+
import {
14+
DownloadTableCSVData,
15+
} from '@cubejs-backend/base-driver';
1416
import {
1517
JDBCDriver,
1618
JDBCDriverConfiguration,
@@ -192,23 +194,6 @@ export class DatabricksDriver extends JDBCDriver {
192194
return result;
193195
}
194196

195-
private async queryColumnTypes(sql: string, params: unknown[]) {
196-
const result = [];
197-
// eslint-disable-next-line camelcase
198-
const response = await this.query<{col_name: string; data_type: string}>(`DESCRIBE QUERY ${sql}`, params);
199-
200-
for (const column of response) {
201-
// Databricks describe additional info by default after empty line.
202-
if (column.col_name === '') {
203-
break;
204-
}
205-
206-
result.push({ name: column.col_name, type: this.toGenericType(column.data_type) });
207-
}
208-
209-
return result;
210-
}
211-
212197
public async getTablesQuery(schemaName: string) {
213198
const response = await this.query(`SHOW TABLES IN ${this.quoteIdentifier(schemaName)}`, []);
214199

@@ -263,22 +248,21 @@ export class DatabricksDriver extends JDBCDriver {
263248
return this.config.exportBucket !== undefined;
264249
}
265250

266-
public async unload(tableName: string, options: UnloadOptions) {
267-
if (!['azure', 's3'].includes(this.config.bucketType as string)) {
268-
throw new Error(`Unsupported export bucket type: ${
269-
this.config.bucketType
270-
}`);
271-
}
272-
273-
const types = options.query ?
274-
await this.unloadWithSql(tableName, options.query.sql, options.query.params) :
275-
await this.unloadWithTable(tableName);
276-
251+
/**
252+
* Saves pre-aggs table to the bucket and returns links to download
253+
* results.
254+
*/
255+
public async unload(
256+
tableName: string,
257+
): Promise<DownloadTableCSVData> {
258+
const types = await this.tableColumnTypes(tableName);
259+
const columns = types.map(t => t.name).join(', ');
277260
const pathname = `${this.config.exportBucket}/${tableName}.csv`;
278261
const csvFile = await this.getCsvFiles(
262+
tableName,
263+
columns,
279264
pathname,
280265
);
281-
282266
return {
283267
csvFile,
284268
types,
@@ -287,41 +271,21 @@ export class DatabricksDriver extends JDBCDriver {
287271
}
288272

289273
/**
290-
* Create table with query and unload it to bucket
291-
*/
292-
private async unloadWithSql(tableName: string, sql: string, params: unknown[]) {
293-
const types = await this.queryColumnTypes(sql, params);
294-
295-
await this.createExternalTableFromSql(tableName, sql, params);
296-
297-
return types;
298-
}
299-
300-
/**
301-
* Create table from preaggregation table with location and unload it to bucket
302-
*/
303-
private async unloadWithTable(tableName: string) {
304-
const types = await this.tableColumnTypes(tableName);
305-
const columns = types.map(t => t.name).join(', ');
306-
307-
await this.createExternalTableFromTable(tableName, columns);
308-
309-
return types;
310-
}
311-
312-
/**
313-
* return csv files signed URLs array.
274+
* Unload table to bucket using Databricks JDBC query and returns (async)
275+
* csv files signed URLs array.
314276
*/
315277
private async getCsvFiles(
278+
table: string,
279+
columns: string,
316280
pathname: string,
317281
): Promise<string[]> {
318282
let res;
319283
switch (this.config.bucketType) {
320284
case 'azure':
321-
res = await this.getSignedAzureUrls(pathname);
285+
res = await this.getAzureCsvFiles(table, columns, pathname);
322286
break;
323287
case 's3':
324-
res = await this.getSignedS3Urls(pathname);
288+
res = await this.getS3CsvFiles(table, columns, pathname);
325289
break;
326290
default:
327291
throw new Error(`Unsupported export bucket type: ${
@@ -331,6 +295,19 @@ export class DatabricksDriver extends JDBCDriver {
331295
return res;
332296
}
333297

298+
/**
299+
* Saves specified table to the Azure blob storage and returns (async)
300+
* csv files signed URLs array.
301+
*/
302+
private async getAzureCsvFiles(
303+
table: string,
304+
columns: string,
305+
pathname: string,
306+
): Promise<string[]> {
307+
await this.createExternalTable(table, columns);
308+
return this.getSignedAzureUrls(pathname);
309+
}
310+
334311
/**
335312
* Returns Azure signed URLs of unloaded scv files.
336313
*/
@@ -383,6 +360,19 @@ export class DatabricksDriver extends JDBCDriver {
383360
return csvFile;
384361
}
385362

363+
/**
364+
* Saves specified table to the S3 bucket and returns (async) csv files
365+
* signed URLs array.
366+
*/
367+
private async getS3CsvFiles(
368+
table: string,
369+
columns: string,
370+
pathname: string,
371+
): Promise<string[]> {
372+
await this.createExternalTable(table, columns);
373+
return this.getSignedS3Urls(pathname);
374+
}
375+
386376
/**
387377
* Returns S3 signed URLs of unloaded scv files.
388378
*/
@@ -439,19 +429,7 @@ export class DatabricksDriver extends JDBCDriver {
439429
* `fs.s3a.access.key <aws-access-key>`
440430
* `fs.s3a.secret.key <aws-secret-key>`
441431
*/
442-
private async createExternalTableFromSql(table: string, sql: string, params: unknown[]) {
443-
await this.query(
444-
`
445-
CREATE TABLE ${table}_csv_export
446-
USING CSV LOCATION '${this.config.exportBucketMountDir || this.config.exportBucket}/${table}.csv'
447-
OPTIONS (escape = '"')
448-
AS (${sql})
449-
`,
450-
params,
451-
);
452-
}
453-
454-
private async createExternalTableFromTable(table: string, columns: string) {
432+
private async createExternalTable(table: string, columns: string,) {
455433
await this.query(
456434
`
457435
CREATE TABLE ${table}_csv_export
@@ -462,8 +440,4 @@ export class DatabricksDriver extends JDBCDriver {
462440
[],
463441
);
464442
}
465-
466-
public capabilities(): DriverCapabilities {
467-
return { unloadWithoutTempTable: true };
468-
}
469443
}

0 commit comments

Comments
 (0)