@@ -3,6 +3,7 @@ import * as path from 'path';
33import stream from 'node:stream' ;
44import util from 'node:util' ;
55import { stringify , NIL } from 'uuid' ;
6+ import Int64 from 'node-int64' ;
67import fetch , { HeadersInit } from 'node-fetch' ;
78import {
89 TSessionHandle ,
@@ -12,7 +13,6 @@ import {
1213 TSparkArrowTypes ,
1314 TSparkParameter ,
1415} from '../thrift/TCLIService_types' ;
15- import { Int64 } from './hive/Types' ;
1616import IDBSQLSession , {
1717 ExecuteStatementOptions ,
1818 TypeInfoRequest ,
@@ -41,22 +41,35 @@ import IClientContext, { ClientConfig } from './contracts/IClientContext';
4141// Explicitly promisify a callback-style `pipeline` because `node:stream/promises` is not available in Node 14
4242const pipeline = util . promisify ( stream . pipeline ) ;
4343
44- const defaultMaxRows = 100000 ;
45-
4644interface OperationResponseShape {
4745 status : TStatus ;
4846 operationHandle ?: TOperationHandle ;
4947 directResults ?: TSparkDirectResults ;
5048}
5149
52- function getDirectResultsOptions ( maxRows : number | null = defaultMaxRows ) {
50+ export function numberToInt64 ( value : number | bigint | Int64 ) : Int64 {
51+ if ( value instanceof Int64 ) {
52+ return value ;
53+ }
54+
55+ if ( typeof value === 'bigint' ) {
56+ const buffer = new ArrayBuffer ( BigInt64Array . BYTES_PER_ELEMENT ) ;
57+ const view = new DataView ( buffer ) ;
58+ view . setBigInt64 ( 0 , value , false ) ; // `false` to use big-endian order
59+ return new Int64 ( Buffer . from ( buffer ) ) ;
60+ }
61+
62+ return new Int64 ( value ) ;
63+ }
64+
65+ function getDirectResultsOptions ( maxRows : number | bigint | Int64 | null | undefined , config : ClientConfig ) {
5366 if ( maxRows === null ) {
5467 return { } ;
5568 }
5669
5770 return {
5871 getDirectResults : {
59- maxRows : new Int64 ( maxRows ) ,
72+ maxRows : numberToInt64 ( maxRows ?? config . directResultsDefaultMaxRows ) ,
6073 } ,
6174 } ;
6275}
@@ -86,7 +99,6 @@ function getArrowOptions(config: ClientConfig): {
8699}
87100
88101function getQueryParameters (
89- sessionHandle : TSessionHandle ,
90102 namedParameters ?: Record < string , DBSQLParameter | DBSQLParameterValue > ,
91103 ordinalParameters ?: Array < DBSQLParameter | DBSQLParameterValue > ,
92104) : Array < TSparkParameter > {
@@ -184,12 +196,12 @@ export default class DBSQLSession implements IDBSQLSession {
184196 const operationPromise = driver . executeStatement ( {
185197 sessionHandle : this . sessionHandle ,
186198 statement,
187- queryTimeout : options . queryTimeout ,
199+ queryTimeout : options . queryTimeout ? numberToInt64 ( options . queryTimeout ) : undefined ,
188200 runAsync : true ,
189- ...getDirectResultsOptions ( options . maxRows ) ,
201+ ...getDirectResultsOptions ( options . maxRows , clientConfig ) ,
190202 ...getArrowOptions ( clientConfig ) ,
191203 canDownloadResult : options . useCloudFetch ?? clientConfig . useCloudFetch ,
192- parameters : getQueryParameters ( this . sessionHandle , options . namedParameters , options . ordinalParameters ) ,
204+ parameters : getQueryParameters ( options . namedParameters , options . ordinalParameters ) ,
193205 canDecompressLZ4Result : clientConfig . useLZ4Compression && Boolean ( LZ4 ) ,
194206 } ) ;
195207 const response = await this . handleResponse ( operationPromise ) ;
@@ -339,10 +351,11 @@ export default class DBSQLSession implements IDBSQLSession {
339351 public async getTypeInfo ( request : TypeInfoRequest = { } ) : Promise < IOperation > {
340352 await this . failIfClosed ( ) ;
341353 const driver = await this . context . getDriver ( ) ;
354+ const clientConfig = this . context . getConfig ( ) ;
342355 const operationPromise = driver . getTypeInfo ( {
343356 sessionHandle : this . sessionHandle ,
344357 runAsync : true ,
345- ...getDirectResultsOptions ( request . maxRows ) ,
358+ ...getDirectResultsOptions ( request . maxRows , clientConfig ) ,
346359 } ) ;
347360 const response = await this . handleResponse ( operationPromise ) ;
348361 return this . createOperation ( response ) ;
@@ -357,10 +370,11 @@ export default class DBSQLSession implements IDBSQLSession {
357370 public async getCatalogs ( request : CatalogsRequest = { } ) : Promise < IOperation > {
358371 await this . failIfClosed ( ) ;
359372 const driver = await this . context . getDriver ( ) ;
373+ const clientConfig = this . context . getConfig ( ) ;
360374 const operationPromise = driver . getCatalogs ( {
361375 sessionHandle : this . sessionHandle ,
362376 runAsync : true ,
363- ...getDirectResultsOptions ( request . maxRows ) ,
377+ ...getDirectResultsOptions ( request . maxRows , clientConfig ) ,
364378 } ) ;
365379 const response = await this . handleResponse ( operationPromise ) ;
366380 return this . createOperation ( response ) ;
@@ -375,12 +389,13 @@ export default class DBSQLSession implements IDBSQLSession {
375389 public async getSchemas ( request : SchemasRequest = { } ) : Promise < IOperation > {
376390 await this . failIfClosed ( ) ;
377391 const driver = await this . context . getDriver ( ) ;
392+ const clientConfig = this . context . getConfig ( ) ;
378393 const operationPromise = driver . getSchemas ( {
379394 sessionHandle : this . sessionHandle ,
380395 catalogName : request . catalogName ,
381396 schemaName : request . schemaName ,
382397 runAsync : true ,
383- ...getDirectResultsOptions ( request . maxRows ) ,
398+ ...getDirectResultsOptions ( request . maxRows , clientConfig ) ,
384399 } ) ;
385400 const response = await this . handleResponse ( operationPromise ) ;
386401 return this . createOperation ( response ) ;
@@ -395,14 +410,15 @@ export default class DBSQLSession implements IDBSQLSession {
395410 public async getTables ( request : TablesRequest = { } ) : Promise < IOperation > {
396411 await this . failIfClosed ( ) ;
397412 const driver = await this . context . getDriver ( ) ;
413+ const clientConfig = this . context . getConfig ( ) ;
398414 const operationPromise = driver . getTables ( {
399415 sessionHandle : this . sessionHandle ,
400416 catalogName : request . catalogName ,
401417 schemaName : request . schemaName ,
402418 tableName : request . tableName ,
403419 tableTypes : request . tableTypes ,
404420 runAsync : true ,
405- ...getDirectResultsOptions ( request . maxRows ) ,
421+ ...getDirectResultsOptions ( request . maxRows , clientConfig ) ,
406422 } ) ;
407423 const response = await this . handleResponse ( operationPromise ) ;
408424 return this . createOperation ( response ) ;
@@ -417,10 +433,11 @@ export default class DBSQLSession implements IDBSQLSession {
417433 public async getTableTypes ( request : TableTypesRequest = { } ) : Promise < IOperation > {
418434 await this . failIfClosed ( ) ;
419435 const driver = await this . context . getDriver ( ) ;
436+ const clientConfig = this . context . getConfig ( ) ;
420437 const operationPromise = driver . getTableTypes ( {
421438 sessionHandle : this . sessionHandle ,
422439 runAsync : true ,
423- ...getDirectResultsOptions ( request . maxRows ) ,
440+ ...getDirectResultsOptions ( request . maxRows , clientConfig ) ,
424441 } ) ;
425442 const response = await this . handleResponse ( operationPromise ) ;
426443 return this . createOperation ( response ) ;
@@ -435,14 +452,15 @@ export default class DBSQLSession implements IDBSQLSession {
435452 public async getColumns ( request : ColumnsRequest = { } ) : Promise < IOperation > {
436453 await this . failIfClosed ( ) ;
437454 const driver = await this . context . getDriver ( ) ;
455+ const clientConfig = this . context . getConfig ( ) ;
438456 const operationPromise = driver . getColumns ( {
439457 sessionHandle : this . sessionHandle ,
440458 catalogName : request . catalogName ,
441459 schemaName : request . schemaName ,
442460 tableName : request . tableName ,
443461 columnName : request . columnName ,
444462 runAsync : true ,
445- ...getDirectResultsOptions ( request . maxRows ) ,
463+ ...getDirectResultsOptions ( request . maxRows , clientConfig ) ,
446464 } ) ;
447465 const response = await this . handleResponse ( operationPromise ) ;
448466 return this . createOperation ( response ) ;
@@ -457,13 +475,14 @@ export default class DBSQLSession implements IDBSQLSession {
457475 public async getFunctions ( request : FunctionsRequest ) : Promise < IOperation > {
458476 await this . failIfClosed ( ) ;
459477 const driver = await this . context . getDriver ( ) ;
478+ const clientConfig = this . context . getConfig ( ) ;
460479 const operationPromise = driver . getFunctions ( {
461480 sessionHandle : this . sessionHandle ,
462481 catalogName : request . catalogName ,
463482 schemaName : request . schemaName ,
464483 functionName : request . functionName ,
465484 runAsync : true ,
466- ...getDirectResultsOptions ( request . maxRows ) ,
485+ ...getDirectResultsOptions ( request . maxRows , clientConfig ) ,
467486 } ) ;
468487 const response = await this . handleResponse ( operationPromise ) ;
469488 return this . createOperation ( response ) ;
@@ -472,13 +491,14 @@ export default class DBSQLSession implements IDBSQLSession {
472491 public async getPrimaryKeys ( request : PrimaryKeysRequest ) : Promise < IOperation > {
473492 await this . failIfClosed ( ) ;
474493 const driver = await this . context . getDriver ( ) ;
494+ const clientConfig = this . context . getConfig ( ) ;
475495 const operationPromise = driver . getPrimaryKeys ( {
476496 sessionHandle : this . sessionHandle ,
477497 catalogName : request . catalogName ,
478498 schemaName : request . schemaName ,
479499 tableName : request . tableName ,
480500 runAsync : true ,
481- ...getDirectResultsOptions ( request . maxRows ) ,
501+ ...getDirectResultsOptions ( request . maxRows , clientConfig ) ,
482502 } ) ;
483503 const response = await this . handleResponse ( operationPromise ) ;
484504 return this . createOperation ( response ) ;
@@ -493,6 +513,7 @@ export default class DBSQLSession implements IDBSQLSession {
493513 public async getCrossReference ( request : CrossReferenceRequest ) : Promise < IOperation > {
494514 await this . failIfClosed ( ) ;
495515 const driver = await this . context . getDriver ( ) ;
516+ const clientConfig = this . context . getConfig ( ) ;
496517 const operationPromise = driver . getCrossReference ( {
497518 sessionHandle : this . sessionHandle ,
498519 parentCatalogName : request . parentCatalogName ,
@@ -502,7 +523,7 @@ export default class DBSQLSession implements IDBSQLSession {
502523 foreignSchemaName : request . foreignSchemaName ,
503524 foreignTableName : request . foreignTableName ,
504525 runAsync : true ,
505- ...getDirectResultsOptions ( request . maxRows ) ,
526+ ...getDirectResultsOptions ( request . maxRows , clientConfig ) ,
506527 } ) ;
507528 const response = await this . handleResponse ( operationPromise ) ;
508529 return this . createOperation ( response ) ;
0 commit comments