44 * @fileoverview The `DatabricksDriver` and related types declaration.
55 */
66
7+ import fetch from 'node-fetch' ;
78import { assertDataSource , getEnv , } from '@cubejs-backend/shared' ;
89import {
910 DatabaseStructure ,
@@ -20,6 +21,7 @@ import { JDBCDriver, JDBCDriverConfiguration, } from '@cubejs-backend/jdbc-drive
2021import { DatabricksQuery } from './DatabricksQuery' ;
2122import {
2223 extractAndRemoveUidPwdFromJdbcUrl ,
24+ parseDatabricksJdbcUrl ,
2325 resolveJDBCDriver
2426} from './helpers' ;
2527
@@ -124,6 +126,11 @@ type ColumnInfo = {
124126 type : GenericDataBaseType ;
125127} ;
126128
129+ export type ParsedConnectionProperties = {
130+ host : string ,
131+ warehouseId : string ,
132+ } ;
133+
127134const DatabricksToGenericType : Record < string , string > = {
128135 binary : 'hll_datasketches' ,
129136 'decimal(10,0)' : 'bigint' ,
@@ -143,6 +150,8 @@ export class DatabricksDriver extends JDBCDriver {
143150 */
144151 protected readonly config : DatabricksDriverConfiguration ;
145152
153+ private readonly parsedConnectionProperties : ParsedConnectionProperties ;
154+
146155 public static dialectClass ( ) {
147156 return DatabricksQuery ;
148157 }
@@ -262,38 +271,46 @@ export class DatabricksDriver extends JDBCDriver {
262271
263272 super ( config ) ;
264273 this . config = config ;
274+ this . parsedConnectionProperties = parseDatabricksJdbcUrl ( url ) ;
265275 this . showSparkProtocolWarn = showSparkProtocolWarn ;
266276 }
267277
268- /**
269- * @override
270- */
271- public readOnly ( ) {
278+ public override readOnly ( ) {
272279 return ! ! this . config . readOnly ;
273280 }
274281
275- /**
276- * @override
277- */
278- public capabilities ( ) : DriverCapabilities {
282+ public override capabilities ( ) : DriverCapabilities {
279283 return {
280284 unloadWithoutTempTable : true ,
281285 incrementalSchemaLoading : true
282286 } ;
283287 }
284288
285- /**
286- * @override
287- */
288- public setLogger ( logger : any ) {
289+ public override setLogger ( logger : any ) {
289290 super . setLogger ( logger ) ;
290291 this . showDeprecations ( ) ;
291292 }
292293
293- /**
294- * @override
295- */
296- public async loadPreAggregationIntoTable (
294+ public override async testConnection ( ) {
295+ const token = `Bearer ${ this . config . properties . PWD } ` ;
296+
297+ const res = await fetch ( `https://${ this . parsedConnectionProperties . host } /api/2.0/sql/warehouses/${ this . parsedConnectionProperties . warehouseId } ` , {
298+ headers : { Authorization : token } ,
299+ } ) ;
300+
301+ if ( ! res . ok ) {
302+ throw new Error ( `Databricks API error: ${ res . statusText } ` ) ;
303+ }
304+
305+ // const _data = await res.json();
306+
307+ // We don't need to check the actual status, because SQL WH will auto resume on real connection
308+ // if (data.state !== 'RUNNING') {
309+ // throw new Error(`Warehouse not running (current state: ${data.state})`);
310+ // }
311+ }
312+
313+ public override async loadPreAggregationIntoTable (
297314 preAggregationTableName : string ,
298315 loadSql : string ,
299316 params : unknown [ ] ,
@@ -320,10 +337,7 @@ export class DatabricksDriver extends JDBCDriver {
320337 }
321338 }
322339
323- /**
324- * @override
325- */
326- public async query < R = unknown > (
340+ public override async query < R = unknown > (
327341 query : string ,
328342 values : unknown [ ] ,
329343 ) : Promise < R [ ] > {
@@ -357,10 +371,7 @@ export class DatabricksDriver extends JDBCDriver {
357371 }
358372 }
359373
360- /**
361- * @override
362- */
363- public dropTable ( tableName : string , options ?: QueryOptions ) : Promise < unknown > {
374+ public override dropTable ( tableName : string , options ?: QueryOptions ) : Promise < unknown > {
364375 const tableFullName = `${
365376 this . config ?. catalog ? `${ this . config . catalog } .` : ''
366377 } ${ tableName } `;
@@ -392,10 +403,7 @@ export class DatabricksDriver extends JDBCDriver {
392403 }
393404 }
394405
395- /**
396- * @override
397- */
398- protected async getCustomClassPath ( ) {
406+ protected override async getCustomClassPath ( ) {
399407 return resolveJDBCDriver ( ) ;
400408 }
401409
0 commit comments