Skip to content

Commit 5c1f75d

Browse files
committed
some fixes and code polishment
1 parent 6bf53a4 commit 5c1f75d

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

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

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,21 @@
44
* @fileoverview The `DatabricksDriver` and related types declaration.
55
*/
66

7+
import { assertDataSource, getEnv, } from '@cubejs-backend/shared';
78
import {
8-
getEnv,
9-
assertDataSource,
10-
} from '@cubejs-backend/shared';
11-
import {
9+
DatabaseStructure,
1210
DriverCapabilities,
11+
GenericDataBaseType,
1312
QueryColumnsResult,
1413
QueryOptions,
1514
QuerySchemasResult,
1615
QueryTablesResult,
17-
UnloadOptions,
18-
GenericDataBaseType,
1916
TableColumn,
20-
DatabaseStructure,
17+
UnloadOptions,
2118
} from '@cubejs-backend/base-driver';
22-
import {
23-
JDBCDriver,
24-
JDBCDriverConfiguration,
25-
} from '@cubejs-backend/jdbc-driver';
19+
import { JDBCDriver, JDBCDriverConfiguration, } from '@cubejs-backend/jdbc-driver';
2620
import { DatabricksQuery } from './DatabricksQuery';
27-
import { resolveJDBCDriver, extractUidFromJdbcUrl } from './helpers';
21+
import { extractUidFromJdbcUrl, resolveJDBCDriver } from './helpers';
2822

2923
const SUPPORTED_BUCKET_TYPES = ['s3', 'gcs', 'azure'];
3024

@@ -440,8 +434,7 @@ export class DatabricksDriver extends JDBCDriver {
440434
metadata[database] = {};
441435
}
442436

443-
const columns = await this.tableColumnTypes(`${database}.${tableName}`);
444-
metadata[database][tableName] = columns;
437+
metadata[database][tableName] = await this.tableColumnTypes(`${database}.${tableName}`);
445438
}));
446439

447440
return metadata;
@@ -538,7 +531,7 @@ export class DatabricksDriver extends JDBCDriver {
538531
* Returns table columns types.
539532
*/
540533
public override async tableColumnTypes(table: string): Promise<TableColumn[]> {
541-
let tableFullName = '';
534+
let tableFullName: string;
542535
const tableArray = table.split('.');
543536

544537
if (tableArray.length === 3) {
@@ -748,11 +741,8 @@ export class DatabricksDriver extends JDBCDriver {
748741
return this.extractFilesFromGCS(
749742
{ credentials: this.config.gcsCredentials },
750743
url.host,
751-
objectSearchPrefix+".csv",
752-
).then(files => files.filter(file =>
753-
decodeURIComponent(new URL(file).pathname).endsWith('.csv') ||
754-
decodeURIComponent(new URL(file).pathname).endsWith('.csv.gz')
755-
));
744+
objectSearchPrefix,
745+
);
756746
} else {
757747
throw new Error(`Unsupported export bucket type: ${
758748
this.config.bucketType
@@ -779,19 +769,22 @@ export class DatabricksDriver extends JDBCDriver {
779769
*
780770
* For Azure blob storage you need to configure account access key in
781771
* Cluster -> Configuration -> Advanced options
782-
* (https://docs.databricks.com/data/data-sources/azure/azure-storage.html#access-azure-blob-storage-directly)
772+
* https://docs.databricks.com/data/data-sources/azure/azure-storage.html#access-azure-blob-storage-directly
783773
*
784774
* `fs.azure.account.key.<storage-account-name>.blob.core.windows.net <storage-account-access-key>`
785775
*
786776
* For S3 bucket storage you need to configure AWS access key and secret in
787777
* Cluster -> Configuration -> Advanced options
788-
* (https://docs.databricks.com/data/data-sources/aws/amazon-s3.html#access-s3-buckets-directly)
778+
* https://docs.databricks.com/data/data-sources/aws/amazon-s3.html#access-s3-buckets-directly
789779
*
790780
* `fs.s3a.access.key <aws-access-key>`
791781
* `fs.s3a.secret.key <aws-secret-key>`
782+
*
792783
* For Google cloud storage you can configure storage credentials and create an external location to access it
793-
* (https://docs.databricks.com/gcp/en/connect/unity-catalog/cloud-storage/storage-credentials
794-
* https://docs.databricks.com/gcp/en/connect/unity-catalog/cloud-storage/external-locations)
784+
* or configure account service key (legacy)
785+
* https://docs.databricks.com/gcp/en/connect/unity-catalog/cloud-storage/storage-credentials
786+
* https://docs.databricks.com/gcp/en/connect/unity-catalog/cloud-storage/external-locations
787+
* https://docs.databricks.com/aws/en/connect/storage/gcs
795788
*/
796789
private async createExternalTableFromSql(tableFullName: string, sql: string, params: unknown[], columns: ColumnInfo[]) {
797790
let select = sql;
@@ -803,15 +796,15 @@ export class DatabricksDriver extends JDBCDriver {
803796
try {
804797
await this.query(
805798
`
806-
CREATE TABLE ${tableFullName}
807-
USING CSV LOCATION '${this.config.exportBucketMountDir || this.config.exportBucket}/${tableFullName}.csv'
799+
CREATE TABLE ${tableFullName}_tmp
800+
USING CSV LOCATION '${this.config.exportBucketMountDir || this.config.exportBucket}/${tableFullName}'
808801
OPTIONS (escape = '"')
809802
AS (${select});
810803
`,
811804
params,
812805
);
813806
} finally {
814-
await this.query(`DROP TABLE IF EXISTS ${tableFullName};`, []);
807+
await this.query(`DROP TABLE IF EXISTS ${tableFullName}_tmp;`, []);
815808
}
816809
}
817810

@@ -821,30 +814,36 @@ export class DatabricksDriver extends JDBCDriver {
821814
*
822815
* For Azure blob storage you need to configure account access key in
823816
* Cluster -> Configuration -> Advanced options
824-
* (https://docs.databricks.com/data/data-sources/azure/azure-storage.html#access-azure-blob-storage-directly)
817+
* https://docs.databricks.com/data/data-sources/azure/azure-storage.html#access-azure-blob-storage-directly
825818
*
826819
* `fs.azure.account.key.<storage-account-name>.blob.core.windows.net <storage-account-access-key>`
827820
*
828821
* For S3 bucket storage you need to configure AWS access key and secret in
829822
* Cluster -> Configuration -> Advanced options
830-
* (https://docs.databricks.com/data/data-sources/aws/amazon-s3.html#access-s3-buckets-directly)
823+
* https://docs.databricks.com/data/data-sources/aws/amazon-s3.html#access-s3-buckets-directly
831824
*
832825
* `fs.s3a.access.key <aws-access-key>`
833826
* `fs.s3a.secret.key <aws-secret-key>`
827+
*
828+
* For Google cloud storage you can configure storage credentials and create an external location to access it
829+
* or configure account service key (legacy)
830+
* https://docs.databricks.com/gcp/en/connect/unity-catalog/cloud-storage/storage-credentials
831+
* https://docs.databricks.com/gcp/en/connect/unity-catalog/cloud-storage/external-locations
832+
* https://docs.databricks.com/aws/en/connect/storage/gcs
834833
*/
835834
private async createExternalTableFromTable(tableFullName: string, columns: ColumnInfo[]) {
836835
try {
837836
await this.query(
838837
`
839-
CREATE TABLE _${tableFullName}
840-
USING CSV LOCATION '${this.config.exportBucketMountDir || this.config.exportBucket}/${tableFullName}.csv'
838+
CREATE TABLE ${tableFullName}_tmp
839+
USING CSV LOCATION '${this.config.exportBucketMountDir || this.config.exportBucket}/${tableFullName}'
841840
OPTIONS (escape = '"')
842841
AS SELECT ${this.generateTableColumnsForExport(columns)} FROM ${tableFullName}
843842
`,
844843
[],
845844
);
846845
} finally {
847-
await this.query(`DROP TABLE IF EXISTS _${tableFullName};`, []);
846+
await this.query(`DROP TABLE IF EXISTS ${tableFullName}_tmp;`, []);
848847
}
849848
}
850849
}

0 commit comments

Comments
 (0)