@@ -5,7 +5,6 @@ import IOperation, {
55 GetSchemaOptions ,
66 WaitUntilReadyOptions ,
77} from '../contracts/IOperation' ;
8- import HiveDriver from '../hive/HiveDriver' ;
98import {
109 TGetOperationStatusResp ,
1110 TOperationHandle ,
@@ -18,19 +17,22 @@ import {
1817} from '../../thrift/TCLIService_types' ;
1918import Status from '../dto/Status' ;
2019import FetchResultsHelper from './FetchResultsHelper' ;
21- import IDBSQLLogger , { LogLevel } from '../contracts/IDBSQLLogger' ;
20+ import { LogLevel } from '../contracts/IDBSQLLogger' ;
2221import OperationStateError , { OperationStateErrorCode } from '../errors/OperationStateError' ;
2322import IOperationResult from '../result/IOperationResult' ;
2423import JsonResult from '../result/JsonResult' ;
2524import ArrowResult from '../result/ArrowResult' ;
2625import CloudFetchResult from '../result/CloudFetchResult' ;
2726import { definedOrError } from '../utils' ;
2827import HiveDriverError from '../errors/HiveDriverError' ;
28+ import IClientContext from '../contracts/IClientContext' ;
2929
3030const defaultMaxRows = 100000 ;
3131
3232interface DBSQLOperationConstructorOptions {
33- logger : IDBSQLLogger ;
33+ handle : TOperationHandle ;
34+ directResults ?: TSparkDirectResults ;
35+ context : IClientContext ;
3436}
3537
3638async function delay ( ms ?: number ) : Promise < void > {
@@ -42,12 +44,10 @@ async function delay(ms?: number): Promise<void> {
4244}
4345
4446export default class DBSQLOperation implements IOperation {
45- private readonly driver : HiveDriver ;
47+ private readonly context : IClientContext ;
4648
4749 private readonly operationHandle : TOperationHandle ;
4850
49- private readonly logger : IDBSQLLogger ;
50-
5151 public onClose ?: ( ) => void ;
5252
5353 private readonly _data : FetchResultsHelper ;
@@ -70,32 +70,26 @@ export default class DBSQLOperation implements IOperation {
7070
7171 private resultHandler ?: IOperationResult ;
7272
73- constructor (
74- driver : HiveDriver ,
75- operationHandle : TOperationHandle ,
76- { logger } : DBSQLOperationConstructorOptions ,
77- directResults ?: TSparkDirectResults ,
78- ) {
79- this . driver = driver ;
80- this . operationHandle = operationHandle ;
81- this . logger = logger ;
73+ constructor ( { handle, directResults, context } : DBSQLOperationConstructorOptions ) {
74+ this . operationHandle = handle ;
75+ this . context = context ;
8276
8377 const useOnlyPrefetchedResults = Boolean ( directResults ?. closeOperation ) ;
8478
85- this . hasResultSet = operationHandle . hasResultSet ;
79+ this . hasResultSet = this . operationHandle . hasResultSet ;
8680 if ( directResults ?. operationStatus ) {
8781 this . processOperationStatusResponse ( directResults . operationStatus ) ;
8882 }
8983
9084 this . metadata = directResults ?. resultSetMetadata ;
9185 this . _data = new FetchResultsHelper (
92- this . driver ,
86+ this . context ,
9387 this . operationHandle ,
9488 [ directResults ?. resultSet ] ,
9589 useOnlyPrefetchedResults ,
9690 ) ;
9791 this . closeOperation = directResults ?. closeOperation ;
98- this . logger . log ( LogLevel . debug , `Operation created with id: ${ this . getId ( ) } ` ) ;
92+ this . context . getLogger ( ) . log ( LogLevel . debug , `Operation created with id: ${ this . getId ( ) } ` ) ;
9993 }
10094
10195 public getId ( ) {
@@ -118,7 +112,7 @@ export default class DBSQLOperation implements IOperation {
118112 const chunk = await this . fetchChunk ( options ) ;
119113 data . push ( chunk ) ;
120114 } while ( await this . hasMoreRows ( ) ) ; // eslint-disable-line no-await-in-loop
121- this . logger ? .log ( LogLevel . debug , `Fetched all data from operation with id: ${ this . getId ( ) } ` ) ;
115+ this . context . getLogger ( ) . log ( LogLevel . debug , `Fetched all data from operation with id: ${ this . getId ( ) } ` ) ;
122116
123117 return data . flat ( ) ;
124118 }
@@ -149,10 +143,12 @@ export default class DBSQLOperation implements IOperation {
149143 await this . failIfClosed ( ) ;
150144
151145 const result = await resultHandler . getValue ( data ? [ data ] : [ ] ) ;
152- this . logger ?. log (
153- LogLevel . debug ,
154- `Fetched chunk of size: ${ options ?. maxRows || defaultMaxRows } from operation with id: ${ this . getId ( ) } ` ,
155- ) ;
146+ this . context
147+ . getLogger ( )
148+ . log (
149+ LogLevel . debug ,
150+ `Fetched chunk of size: ${ options ?. maxRows || defaultMaxRows } from operation with id: ${ this . getId ( ) } ` ,
151+ ) ;
156152 return result ;
157153 }
158154
@@ -163,13 +159,14 @@ export default class DBSQLOperation implements IOperation {
163159 */
164160 public async status ( progress : boolean = false ) : Promise < TGetOperationStatusResp > {
165161 await this . failIfClosed ( ) ;
166- this . logger ? .log ( LogLevel . debug , `Fetching status for operation with id: ${ this . getId ( ) } ` ) ;
162+ this . context . getLogger ( ) . log ( LogLevel . debug , `Fetching status for operation with id: ${ this . getId ( ) } ` ) ;
167163
168164 if ( this . operationStatus ) {
169165 return this . operationStatus ;
170166 }
171167
172- const response = await this . driver . getOperationStatus ( {
168+ const driver = await this . context . getDriver ( ) ;
169+ const response = await driver . getOperationStatus ( {
173170 operationHandle : this . operationHandle ,
174171 getProgressUpdate : progress ,
175172 } ) ;
@@ -186,9 +183,10 @@ export default class DBSQLOperation implements IOperation {
186183 return Status . success ( ) ;
187184 }
188185
189- this . logger ? .log ( LogLevel . debug , `Cancelling operation with id: ${ this . getId ( ) } ` ) ;
186+ this . context . getLogger ( ) . log ( LogLevel . debug , `Cancelling operation with id: ${ this . getId ( ) } ` ) ;
190187
191- const response = await this . driver . cancelOperation ( {
188+ const driver = await this . context . getDriver ( ) ;
189+ const response = await driver . cancelOperation ( {
192190 operationHandle : this . operationHandle ,
193191 } ) ;
194192 Status . assert ( response . status ) ;
@@ -209,11 +207,12 @@ export default class DBSQLOperation implements IOperation {
209207 return Status . success ( ) ;
210208 }
211209
212- this . logger ? .log ( LogLevel . debug , `Closing operation with id: ${ this . getId ( ) } ` ) ;
210+ this . context . getLogger ( ) . log ( LogLevel . debug , `Closing operation with id: ${ this . getId ( ) } ` ) ;
213211
212+ const driver = await this . context . getDriver ( ) ;
214213 const response =
215214 this . closeOperation ??
216- ( await this . driver . closeOperation ( {
215+ ( await driver . closeOperation ( {
217216 operationHandle : this . operationHandle ,
218217 } ) ) ;
219218 Status . assert ( response . status ) ;
@@ -254,7 +253,7 @@ export default class DBSQLOperation implements IOperation {
254253
255254 await this . waitUntilReady ( options ) ;
256255
257- this . logger ? .log ( LogLevel . debug , `Fetching schema for operation with id: ${ this . getId ( ) } ` ) ;
256+ this . context . getLogger ( ) . log ( LogLevel . debug , `Fetching schema for operation with id: ${ this . getId ( ) } ` ) ;
258257 const metadata = await this . fetchMetadata ( ) ;
259258 return metadata . schema ?? null ;
260259 }
@@ -332,7 +331,8 @@ export default class DBSQLOperation implements IOperation {
332331
333332 private async fetchMetadata ( ) {
334333 if ( ! this . metadata ) {
335- const metadata = await this . driver . getResultSetMetadata ( {
334+ const driver = await this . context . getDriver ( ) ;
335+ const metadata = await driver . getResultSetMetadata ( {
336336 operationHandle : this . operationHandle ,
337337 } ) ;
338338 Status . assert ( metadata . status ) ;
@@ -349,13 +349,13 @@ export default class DBSQLOperation implements IOperation {
349349 if ( ! this . resultHandler ) {
350350 switch ( resultFormat ) {
351351 case TSparkRowSetType . COLUMN_BASED_SET :
352- this . resultHandler = new JsonResult ( metadata . schema ) ;
352+ this . resultHandler = new JsonResult ( this . context , metadata . schema ) ;
353353 break ;
354354 case TSparkRowSetType . ARROW_BASED_SET :
355- this . resultHandler = new ArrowResult ( metadata . schema , metadata . arrowSchema ) ;
355+ this . resultHandler = new ArrowResult ( this . context , metadata . schema , metadata . arrowSchema ) ;
356356 break ;
357357 case TSparkRowSetType . URL_BASED_SET :
358- this . resultHandler = new CloudFetchResult ( metadata . schema ) ;
358+ this . resultHandler = new CloudFetchResult ( this . context , metadata . schema ) ;
359359 break ;
360360 default :
361361 this . resultHandler = undefined ;
0 commit comments