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,50 @@ 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+ if ( [ 'DELETING' , 'DELETED' ] . includes ( data . state ) ) {
308+ throw new Error ( `Warehouse is being deleted (current state: ${ data . state } )` ) ;
309+ }
310+
311+ // There is also DEGRADED status, but it doesn't mean that cluster is 100% not working...
312+ if ( data . health ?. status === 'FAILED' ) {
313+ throw new Error ( `Warehouse is unhealthy: ${ data . health ?. summary } . Details: ${ data . health ?. details } ` ) ;
314+ }
315+ }
316+
317+ public override async loadPreAggregationIntoTable (
297318 preAggregationTableName : string ,
298319 loadSql : string ,
299320 params : unknown [ ] ,
@@ -320,10 +341,7 @@ export class DatabricksDriver extends JDBCDriver {
320341 }
321342 }
322343
323- /**
324- * @override
325- */
326- public async query < R = unknown > (
344+ public override async query < R = unknown > (
327345 query : string ,
328346 values : unknown [ ] ,
329347 ) : Promise < R [ ] > {
@@ -357,10 +375,7 @@ export class DatabricksDriver extends JDBCDriver {
357375 }
358376 }
359377
360- /**
361- * @override
362- */
363- public dropTable ( tableName : string , options ?: QueryOptions ) : Promise < unknown > {
378+ public override dropTable ( tableName : string , options ?: QueryOptions ) : Promise < unknown > {
364379 const tableFullName = `${
365380 this . config ?. catalog ? `${ this . config . catalog } .` : ''
366381 } ${ tableName } `;
@@ -392,10 +407,7 @@ export class DatabricksDriver extends JDBCDriver {
392407 }
393408 }
394409
395- /**
396- * @override
397- */
398- protected async getCustomClassPath ( ) {
410+ protected override async getCustomClassPath ( ) {
399411 return resolveJDBCDriver ( ) ;
400412 }
401413
0 commit comments