-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathexecution.test.ts
More file actions
1388 lines (1216 loc) · 56.4 KB
/
execution.test.ts
File metadata and controls
1388 lines (1216 loc) · 56.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2026 Databricks, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { expect } from 'chai';
import sinon from 'sinon';
import Int64 from 'node-int64';
import expectNativeConnectionOptions from './_helpers/nativeOptions';
import KernelBackend from '../../../lib/kernel/KernelBackend';
import KernelSessionBackend from '../../../lib/kernel/KernelSessionBackend';
import KernelOperationBackend from '../../../lib/kernel/KernelOperationBackend';
import { KernelNativeBinding, KernelConnection, KernelStatement } from '../../../lib/kernel/KernelNativeLoader';
import IClientContext, { ClientConfig } from '../../../lib/contracts/IClientContext';
import IDBSQLLogger, { LogLevel } from '../../../lib/contracts/IDBSQLLogger';
import HiveDriverError from '../../../lib/errors/HiveDriverError';
import ParameterError from '../../../lib/errors/ParameterError';
import OperationStateError, { OperationStateErrorCode } from '../../../lib/errors/OperationStateError';
import { ConnectionOptions } from '../../../lib/contracts/IDBSQLClient';
import { OperationState } from '../../../lib/contracts/OperationStatus';
// -----------------------------------------------------------------------------
// Fakes — minimal stand-ins for the napi-rs generated surface and the
// IClientContext side of the abstraction. Keeping them inline avoids
// pulling in test-only fixtures from outside the kernel/ namespace.
// -----------------------------------------------------------------------------
class FakeNativeStatement implements KernelStatement {
public closed = false;
public cancelled = false;
// Mirrors the kernel `Statement.statementId` getter.
public readonly statementId = '01ef-fake-statement-id';
public async fetchNextBatch() {
return null;
}
// schema() is synchronous on the merged-kernel binding.
public schema() {
return { ipcBytes: Buffer.alloc(0) };
}
public async cancel() {
this.cancelled = true;
}
public async close() {
this.closed = true;
}
// Status accessors added by the kernel's status-fields surface. The values
// are configurable so a test can assert non-null rich-status (e.g. a DML
// `numModifiedRows`) propagates through `op.status()`; they default to all-
// null, matching a SELECT / metadata statement that carries none.
public rich: KernelRichStatusValues = {
numModifiedRows: null,
displayMessage: null,
diagnosticInfo: null,
errorDetailsJson: null,
};
// Counts every rich-field accessor call so a test can assert the backend
// memoizes the read on a terminal statement (re-`status()` must not re-hit
// the FFI accessors).
public richReads = 0;
public async numModifiedRows(): Promise<number | null> {
this.richReads += 1;
return this.rich.numModifiedRows;
}
public async displayMessage(): Promise<string | null> {
this.richReads += 1;
return this.rich.displayMessage;
}
public async diagnosticInfo(): Promise<string | null> {
this.richReads += 1;
return this.rich.diagnosticInfo;
}
public async errorDetailsJson(): Promise<string | null> {
this.richReads += 1;
return this.rich.errorDetailsJson;
}
}
interface KernelRichStatusValues {
numModifiedRows: number | null;
displayMessage: string | null;
diagnosticInfo: string | null;
errorDetailsJson: string | null;
}
/**
* Fake `AsyncStatement` (the `submitStatement` return). `status()` reports a
* configurable state (default Succeeded); `awaitResult()` yields a fetch handle
* (reuses `FakeNativeStatement`'s fetchNextBatch/schema surface).
*/
class FakeAsyncStatement {
public cancelled = false;
public closed = false;
public statusCalls = 0;
public awaitResultError: Error | null = null;
// Successive status() returns drain this queue; the last value sticks.
private readonly states: string[];
public readonly statementId = '01ef-fake-async-id';
constructor(
statusValue: string | string[] = 'Succeeded',
public readonly resultHandle: FakeNativeStatement = new FakeNativeStatement(),
) {
this.states = Array.isArray(statusValue) ? [...statusValue] : [statusValue];
}
public async status(): Promise<string> {
this.statusCalls += 1;
return this.states.length > 1 ? (this.states.shift() as string) : this.states[0];
}
public async awaitResult(): Promise<FakeNativeStatement> {
if (this.awaitResultError) {
throw this.awaitResultError;
}
return this.resultHandle;
}
public async cancel(): Promise<void> {
this.cancelled = true;
}
public async close(): Promise<void> {
this.closed = true;
}
// Extended status accessors exposed on the napi AsyncStatement. The kernel
// populates them off the terminal GetStatement poll (a DML's count rides on
// that response); configurable here to assert the driver surfaces them
// through op.status() on the async path.
public rich: KernelRichStatusValues = {
numModifiedRows: null,
displayMessage: null,
diagnosticInfo: null,
errorDetailsJson: null,
};
public async numModifiedRows(): Promise<number | null> {
return this.rich.numModifiedRows;
}
public async displayMessage(): Promise<string | null> {
return this.rich.displayMessage;
}
public async diagnosticInfo(): Promise<string | null> {
return this.rich.diagnosticInfo;
}
public async errorDetailsJson(): Promise<string | null> {
return this.rich.errorDetailsJson;
}
}
/**
* Fake `CancellableExecution` (the `executeStatementCancellable` return — the
* sync `runAsync: false` query path). `result()` drives the (already-terminal,
* in the fake) execution and yields the terminal statement fetch handle;
* `cancel()` flips a flag and, if armed, makes a pending `result()` reject with
* a Cancelled-shaped kernel error to model mid-compute interruption.
*/
class FakeCancellableExecution {
public cancelled = false;
public resultCalls = 0;
public resultError: Error | null = null;
// Mirrors the real `CancellableExecution.statementId`: `null` until the
// initial execute round-trip publishes the server id mid-`result()`. The
// resolved `Statement` (resultHandle) carries the id (`FakeNativeStatement`).
public readonly statementId: string | null = null;
// When set, the result() promise stays pending until cancel() rejects it,
// modelling a still-running blocking execute that a concurrent cancel aborts.
private pendingResolve?: (stmt: FakeNativeStatement) => void;
private pendingReject?: (err: Error) => void;
constructor(public readonly resultHandle: FakeNativeStatement = new FakeNativeStatement()) {}
// When true, result() does not resolve until cancel()/an error fires.
public block = false;
public async result(): Promise<FakeNativeStatement> {
this.resultCalls += 1;
if (this.resultError) {
throw this.resultError;
}
if (this.block) {
return new Promise<FakeNativeStatement>((resolve, reject) => {
this.pendingResolve = resolve;
this.pendingReject = reject;
});
}
return this.resultHandle;
}
public async cancel(): Promise<void> {
this.cancelled = true;
// Model the server flipping the statement terminal: a parked result()
// rejects with the kernel's Cancelled error envelope.
if (this.pendingReject) {
const err = new Error('statement cancelled');
this.pendingReject(err);
this.pendingReject = undefined;
this.pendingResolve = undefined;
}
}
// Resolve a parked (blocked) result() with the terminal statement, modelling
// the server-side blocking execute finally completing.
public release(): void {
if (this.pendingResolve) {
this.pendingResolve(this.resultHandle);
this.pendingResolve = undefined;
this.pendingReject = undefined;
}
}
}
class FakeNativeConnection implements KernelConnection {
public closed = false;
public lastSql?: string;
// Records the per-statement options object passed to executeStatement
// (undefined for the no-options path) so param-forwarding can be asserted.
public lastOptions?: unknown;
// Records every metadata call as `[method, ...args]` so the session
// backend's request → napi-argument mapping can be asserted.
public metadataCalls: Array<unknown[]> = [];
public throwOnExecute: Error | null = null;
public statementToReturn: FakeNativeStatement = new FakeNativeStatement();
// Mirrors the kernel `Connection.sessionId` getter.
public readonly sessionId = '01ef-fake-session-id';
// Last AsyncStatement handed out by submitStatement (the async query path).
public lastAsyncStatement?: FakeAsyncStatement;
// The async submit state(s) the next FakeAsyncStatement should report.
public submitStatusValue: string | string[] = 'Succeeded';
// Last CancellableExecution handed out by executeStatementCancellable (the
// sync `runAsync: false` query path — the DEFAULT).
public lastCancellableExecution?: FakeCancellableExecution;
// The bare blocking executeStatement path: the kernel backend's sync default
// routes through executeStatementCancellable (below), but the binding still
// exposes this for completeness.
public async executeStatement(sql: string, options?: unknown): Promise<KernelStatement> {
if (this.throwOnExecute) {
throw this.throwOnExecute;
}
this.lastSql = sql;
this.lastOptions = options;
return this.statementToReturn;
}
// Rich status the next sync-execute's terminal Statement should report
// (e.g. a DML `numModifiedRows`). Defaults to all-null (a SELECT).
public richStatus?: KernelRichStatusValues;
// Sync (`runAsync: false`, the DEFAULT) query path: records sql + options and
// returns a pending CancellableExecution whose result() drives the execute.
public async executeStatementCancellable(sql: string, options?: unknown): Promise<any> {
if (this.throwOnExecute) {
throw this.throwOnExecute;
}
this.lastSql = sql;
this.lastOptions = options;
const resultHandle = new FakeNativeStatement();
if (this.richStatus) {
resultHandle.rich = this.richStatus;
}
this.lastCancellableExecution = new FakeCancellableExecution(resultHandle);
return this.lastCancellableExecution;
}
// directResults (`runAsync: false`, the DEFAULT) query path: records sql +
// options and returns either a terminal `Statement` (Completed arm) or — when
// `directReturnsRunning` is set — a pending `AsyncStatement` (Running arm),
// the two arms `KernelSessionBackend.executeStatement` feature-detects via
// `awaitResult`.
public directReturnsRunning = false;
public async executeStatementDirect(sql: string, options?: unknown): Promise<any> {
if (this.throwOnExecute) {
throw this.throwOnExecute;
}
this.lastSql = sql;
this.lastOptions = options;
if (this.directReturnsRunning) {
this.lastAsyncStatement = new FakeAsyncStatement(this.submitStatusValue);
return this.lastAsyncStatement;
}
return this.statementToReturn;
}
// Async-submit path: records sql + per-statement options (for forwarding
// assertions) and returns a pending AsyncStatement.
public async submitStatement(sql: string, options?: unknown): Promise<any> {
if (this.throwOnExecute) {
throw this.throwOnExecute;
}
this.lastSql = sql;
this.lastOptions = options;
this.lastAsyncStatement = new FakeAsyncStatement(this.submitStatusValue);
return this.lastAsyncStatement;
}
private recordMetadata(method: string, args: unknown[]): Promise<KernelStatement> {
this.metadataCalls.push([method, ...args]);
return Promise.resolve(this.statementToReturn);
}
public listProcedures(catalog?: unknown, schemaPattern?: unknown, procedurePattern?: unknown) {
return this.recordMetadata('listProcedures', [catalog, schemaPattern, procedurePattern]);
}
public listCatalogs() {
return this.recordMetadata('listCatalogs', []);
}
public listSchemas(catalog?: unknown, schemaPattern?: unknown) {
return this.recordMetadata('listSchemas', [catalog, schemaPattern]);
}
public listTables(catalog?: unknown, schemaPattern?: unknown, tablePattern?: unknown, tableTypes?: unknown) {
return this.recordMetadata('listTables', [catalog, schemaPattern, tablePattern, tableTypes]);
}
public listColumns(catalog?: unknown, schemaPattern?: unknown, tablePattern?: unknown, columnPattern?: unknown) {
return this.recordMetadata('listColumns', [catalog, schemaPattern, tablePattern, columnPattern]);
}
public listFunctions(catalog?: unknown, schemaPattern?: unknown, functionPattern?: unknown) {
return this.recordMetadata('listFunctions', [catalog, schemaPattern, functionPattern]);
}
public listTableTypes() {
return this.recordMetadata('listTableTypes', []);
}
public listTypeInfo() {
return this.recordMetadata('listTypeInfo', []);
}
public getPrimaryKeys(catalog: unknown, schema: unknown, table: unknown) {
return this.recordMetadata('getPrimaryKeys', [catalog, schema, table]);
}
public getCrossReference(
parentCatalog: unknown,
parentSchema: unknown,
parentTable: unknown,
foreignCatalog: unknown,
foreignSchema: unknown,
foreignTable: unknown,
) {
return this.recordMetadata('getCrossReference', [
parentCatalog,
parentSchema,
parentTable,
foreignCatalog,
foreignSchema,
foreignTable,
]);
}
public async close(): Promise<void> {
this.closed = true;
}
}
function makeBinding(connection: KernelConnection): KernelNativeBinding & {
openSessionStub: sinon.SinonStub;
} {
const openSessionStub = sinon.stub().resolves(connection);
// Structural cast through `unknown`: the binding type carries an `AuthMode`
// const enum that can't be produced as a runtime value, so the whole fake
// is cast rather than each member.
const binding = {
version: () => 'test',
openSession: openSessionStub,
Connection: function Connection() {},
Statement: function Statement() {},
} as unknown as KernelNativeBinding;
return Object.assign(binding, { openSessionStub });
}
function makeContext(logger?: IDBSQLLogger): IClientContext {
const log: IDBSQLLogger = logger ?? {
log(_level: LogLevel, _message: string): void {
// no-op
},
};
const config = {} as ClientConfig;
return {
getConfig: () => config,
getLogger: () => log,
getConnectionProvider: async () => {
throw new Error('not used by kernel backend');
},
getClient: async () => {
throw new Error('not used by kernel backend');
},
getDriver: async () => {
throw new Error('not used by kernel backend');
},
};
}
// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
describe('KernelBackend', () => {
it('connect() captures the connection options and validates PAT auth', async () => {
const connection = new FakeNativeConnection();
const binding = makeBinding(connection);
const backend = new KernelBackend({ context: makeContext(), nativeBinding: binding });
await backend.connect({
host: 'example.databricks.com',
path: '/sql/1.0/warehouses/abc',
token: 'dapi-token',
} as ConnectionOptions);
// openSession should not have been called by connect()
expect(binding.openSessionStub.called).to.equal(false);
});
// kernel-auth-u2m: `databricks-oauth` with no id/secret is now the U2M happy
// path (M0 was PAT-only, but the OAuth M2M+U2M feature on kernel-auth-u2m
// accepts the full set of `databricks-oauth` variants). M2M/U2M flow-
// dispatch coverage lives in auth-m2m.test.ts / auth-u2m.test.ts;
// out-of-scope auth modes are now whatever neither PAT nor
// `databricks-oauth` covers (e.g. `token-provider`, `external-token`).
it('connect() rejects unsupported auth modes (non-PAT, non-OAuth)', async () => {
const connection = new FakeNativeConnection();
const binding = makeBinding(connection);
const backend = new KernelBackend({ context: makeContext(), nativeBinding: binding });
let thrown: unknown;
try {
await backend.connect({
host: 'example.databricks.com',
path: '/sql/1.0/warehouses/abc',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
authType: 'token-provider',
} as any);
} catch (err) {
thrown = err;
}
expect(thrown).to.be.instanceOf(HiveDriverError);
expect((thrown as Error).message).to.match(/unsupported auth mode/);
});
it('connect() rejects missing token', async () => {
const connection = new FakeNativeConnection();
const binding = makeBinding(connection);
const backend = new KernelBackend({ context: makeContext(), nativeBinding: binding });
let thrown: unknown;
try {
await backend.connect({
host: 'example.databricks.com',
path: '/sql/1.0/warehouses/abc',
token: '',
} as ConnectionOptions);
} catch (err) {
thrown = err;
}
expect(thrown).to.be.instanceOf(HiveDriverError);
// After kernel-integration merge, missing-token validation goes through
// KernelAuth.buildKernelConnectionOptions which throws AuthenticationError
// (extends HiveDriverError) with the "non-empty PAT" message.
expect((thrown as Error).message).to.match(/non-empty PAT/);
});
it('openSession() throws if connect() was not called', async () => {
const connection = new FakeNativeConnection();
const binding = makeBinding(connection);
const backend = new KernelBackend({ context: makeContext(), nativeBinding: binding });
let thrown: unknown;
try {
await backend.openSession({});
} catch (err) {
thrown = err;
}
expect(thrown).to.be.instanceOf(HiveDriverError);
expect((thrown as Error).message).to.match(/not connected/);
});
it('openSession() forwards hostName / httpPath / token to napi binding', async () => {
const connection = new FakeNativeConnection();
const binding = makeBinding(connection);
const backend = new KernelBackend({ context: makeContext(), nativeBinding: binding });
await backend.connect({
host: 'workspace.example',
path: '/sql/1.0/warehouses/xyz',
token: 'dapi-token',
} as ConnectionOptions);
await backend.openSession({});
expect(binding.openSessionStub.calledOnce).to.equal(true);
const args = binding.openSessionStub.firstCall.args[0];
// kernel-auth-u2m introduced the discriminated KernelNativeConnectionOptions
// shape with a leading `authMode` tag — `'Pat'` for the PAT branch.
// `intervalsAsString: true` is always set so the kernel result shape is a
// byte-compatible drop-in for the Thrift backend (interval-as-string).
expectNativeConnectionOptions(args, {
hostName: 'workspace.example',
httpPath: '/sql/1.0/warehouses/xyz',
authMode: 'Pat',
token: 'dapi-token',
intervalsAsString: true,
});
});
it('openSession() serializes session-level queryTags into sessionConf.QUERY_TAGS', async () => {
const connection = new FakeNativeConnection();
const binding = makeBinding(connection);
const backend = new KernelBackend({ context: makeContext(), nativeBinding: binding });
await backend.connect({ host: 'h', path: '/p', token: 't' } as ConnectionOptions);
await backend.openSession({ queryTags: { team: 'eng', env: 'prod' } });
// Session-level tags land in the reserved QUERY_TAGS session conf (the
// kernel allowlists it → SEA CreateSession session_confs), mirroring Thrift.
const conf = (binding.openSessionStub.firstCall.args[0] as { sessionConf?: Record<string, string> }).sessionConf;
expect(conf?.QUERY_TAGS).to.be.a('string');
expect(conf?.QUERY_TAGS).to.contain('team:eng').and.to.contain('env:prod');
});
it('openSession() queryTags takes precedence over an explicit configuration.QUERY_TAGS', async () => {
const connection = new FakeNativeConnection();
const binding = makeBinding(connection);
const backend = new KernelBackend({ context: makeContext(), nativeBinding: binding });
await backend.connect({ host: 'h', path: '/p', token: 't' } as ConnectionOptions);
await backend.openSession({
configuration: { QUERY_TAGS: 'manual-raw-value' },
queryTags: { team: 'eng' },
});
const conf = (binding.openSessionStub.firstCall.args[0] as { sessionConf?: Record<string, string> }).sessionConf;
expect(conf?.QUERY_TAGS).to.contain('team:eng').and.to.not.equal('manual-raw-value');
});
it('openSession() returns a KernelSessionBackend wrapping the napi Connection', async () => {
const connection = new FakeNativeConnection();
const binding = makeBinding(connection);
const backend = new KernelBackend({ context: makeContext(), nativeBinding: binding });
await backend.connect({
host: 'h',
path: '/p',
token: 't',
} as ConnectionOptions);
const sessionBackend = await backend.openSession({});
expect(sessionBackend).to.be.instanceOf(KernelSessionBackend);
expect(sessionBackend.id).to.be.a('string').and.have.length.greaterThan(0);
});
it('openSession() forwards initialCatalog / initialSchema / configuration to the napi openSession call (not per-statement)', async () => {
const connection = new FakeNativeConnection();
const binding = makeBinding(connection);
const backend = new KernelBackend({ context: makeContext(), nativeBinding: binding });
await backend.connect({
host: 'h',
path: '/p',
token: 't',
} as ConnectionOptions);
const session = await backend.openSession({
initialCatalog: 'main',
initialSchema: 'default',
configuration: { 'spark.sql.execution.arrow.enabled': 'true' },
});
// The defaults reach the kernel via `Session::builder().defaults()` +
// `.session_conf()`, applied on `CreateSession`. Assert they were
// folded into the napi `openSession` arg.
expect(binding.openSessionStub.calledOnce).to.equal(true);
expect(binding.openSessionStub.firstCall.args[0]).to.deep.include({
authMode: 'Pat',
token: 't',
catalog: 'main',
schema: 'default',
sessionConf: { 'spark.sql.execution.arrow.enabled': 'true' },
});
// And the SQL still threads through executeStatement (now with no
// per-statement options).
await session.executeStatement('SELECT 1', {});
expect(connection.lastSql).to.equal('SELECT 1');
});
it('close() clears connection state without throwing', async () => {
const connection = new FakeNativeConnection();
const binding = makeBinding(connection);
const backend = new KernelBackend({ context: makeContext(), nativeBinding: binding });
await backend.connect({ host: 'h', path: '/p', token: 't' } as ConnectionOptions);
await backend.close();
let thrown: unknown;
try {
await backend.openSession({});
} catch (err) {
thrown = err;
}
expect(thrown).to.be.instanceOf(HiveDriverError);
});
});
describe('KernelSessionBackend', () => {
function makeSession(connection: KernelConnection) {
return new KernelSessionBackend({ connection, context: makeContext() });
}
it('executeStatement passes sql through verbatim', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.executeStatement('SELECT * FROM foo', {});
expect(connection.lastSql).to.equal('SELECT * FROM foo');
});
it('executeStatement returns a KernelOperationBackend with an id', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
const op = await session.executeStatement('SELECT 1', {});
expect(op).to.be.instanceOf(KernelOperationBackend);
expect(op.id).to.be.a('string').and.have.length.greaterThan(0);
});
it('executeStatement (sync default) routes a still-running query through the AsyncStatement arm', async () => {
// The directResults Running arm — a query that did NOT finish within the
// server inline wait comes back as an AsyncStatement (poll/cancel handle).
// This is the branch the whole PR exists to add (Node mid-run cancel).
const connection = new FakeNativeConnection();
connection.directReturnsRunning = true;
const session = makeSession(connection);
const op = await session.executeStatement('SELECT slow', {});
expect(op).to.be.instanceOf(KernelOperationBackend);
// The Running arm was taken: an AsyncStatement was constructed + wired
// (not the terminal `statement` arm).
expect(connection.lastAsyncStatement, 'AsyncStatement (Running) arm should be taken').to.not.equal(undefined);
// Driving the op polls the async handle's status() — the polling arm.
await op.waitUntilReady();
expect(connection.lastAsyncStatement!.statusCalls, 'async handle polled via status()').to.be.greaterThan(0);
});
it('executeStatement (sync default) AsyncStatement arm: op.cancel() reaches the running statement', async () => {
// The point of directResults on single-threaded Node: the returned op holds
// a handle to the still-running statement, so op.cancel() can abort it.
const connection = new FakeNativeConnection();
connection.directReturnsRunning = true;
const session = makeSession(connection);
const op = await session.executeStatement('SELECT slow', {});
await op.cancel();
expect(connection.lastAsyncStatement!.cancelled, 'cancel reaches the running statement').to.equal(true);
});
it('executeStatement (sync default) routes a fast query through the terminal Statement arm', async () => {
// Contrast: a query that finished within the inline wait comes back as a
// terminal Statement (result inline) — no AsyncStatement is created.
const connection = new FakeNativeConnection(); // directReturnsRunning = false (default)
const session = makeSession(connection);
const op = await session.executeStatement('SELECT 1', {});
expect(connection.lastAsyncStatement, 'no AsyncStatement arm for a terminal query').to.equal(undefined);
await op.cancel();
expect(connection.statementToReturn.cancelled, 'cancel reaches the terminal statement').to.equal(true);
});
it('executeStatement forwards ordinalParameters as napi positionalParams', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.executeStatement('SELECT ?', { ordinalParameters: [42, 'hi'] });
const options = connection.lastOptions as { positionalParams?: Array<{ sqlType: string; value?: string }> };
expect(options, 'options should be passed').to.not.equal(undefined);
expect(options.positionalParams).to.have.length(2);
expect(options.positionalParams?.[0]).to.deep.equal({ sqlType: 'INTEGER', value: '42' });
expect(options.positionalParams?.[1]).to.deep.equal({ sqlType: 'STRING', value: 'hi' });
});
it('executeStatement forwards namedParameters as napi namedParams (:name carried)', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.executeStatement('SELECT :x', { namedParameters: { x: 7 } });
const options = connection.lastOptions as {
namedParams?: Array<{ name: string; sqlType: string; value?: string }>;
};
expect(options.namedParams).to.have.length(1);
expect(options.namedParams?.[0]).to.deep.equal({ name: 'x', sqlType: 'INTEGER', value: '7' });
});
it('executeStatement sends no options object on the no-params path', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.executeStatement('SELECT 1', {});
expect(connection.lastOptions).to.equal(undefined);
});
it('executeStatement rejects mixing ordinal and named parameters with the same ParameterError as Thrift', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
let thrown: unknown;
try {
await session.executeStatement('SELECT ?, :x', { ordinalParameters: [1], namedParameters: { x: 2 } });
} catch (err) {
thrown = err;
}
// Cross-backend parity: ThriftSessionBackend throws ParameterError with this
// exact message, so a caller catching ParameterError behaves identically.
expect(thrown).to.be.instanceOf(ParameterError);
expect((thrown as Error).message).to.equal('Driver does not support both ordinal and named parameters.');
});
it('executeStatement (sync default) does NOT forward queryTimeout — no-op on kernel', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.executeStatement('SELECT 1', { queryTimeout: 30 });
// queryTimeout is a no-op on kernel (SQL Warehouses use STATEMENT_TIMEOUT). It
// must NOT be mapped to the kernel's `wait_timeout` (the inline-hold window),
// so nothing is forwarded onto the napi options.
expect((connection.lastOptions as { queryTimeoutSecs?: number } | undefined)?.queryTimeoutSecs).to.equal(undefined);
});
it('executeStatement (runAsync: true) does NOT forward queryTimeout — no-op on kernel', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.executeStatement('SELECT 1', { queryTimeout: 30, runAsync: true });
expect((connection.lastOptions as { queryTimeoutSecs?: number } | undefined)?.queryTimeoutSecs).to.equal(undefined);
});
it('executeStatement forwards rowLimit', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.executeStatement('SELECT 1', { rowLimit: 100 });
expect((connection.lastOptions as { rowLimit?: number }).rowLimit).to.equal(100);
});
it('executeStatement serialises queryTags into statementConf.query_tags', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.executeStatement('SELECT 1', { queryTags: { team: 'x', env: 'prod' } });
const conf = (connection.lastOptions as { statementConf?: Record<string, string> }).statementConf;
expect(conf).to.have.property('query_tags');
expect(conf?.query_tags).to.contain('team:x').and.to.contain('env:prod');
});
it('executeStatement merges explicit statementConf with serialised queryTags', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.executeStatement('SELECT 1', {
statementConf: { 'spark.sql.ansi.enabled': 'true' },
queryTags: { team: 'x' },
});
const conf = (connection.lastOptions as { statementConf?: Record<string, string> }).statementConf;
expect(conf?.['spark.sql.ansi.enabled']).to.equal('true');
expect(conf?.query_tags).to.contain('team:x');
});
it('queryTags wins over a query_tags key in statementConf (precedence on collision)', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.executeStatement('SELECT 1', {
statementConf: { query_tags: 'manual-raw-value' },
queryTags: { team: 'x' },
});
const conf = (connection.lastOptions as { statementConf?: Record<string, string> }).statementConf;
// The structured `queryTags` option overwrites a raw `query_tags` conf key —
// a single, predictable wire value rather than two competing ones.
expect(conf?.query_tags).to.contain('team:x').and.to.not.equal('manual-raw-value');
});
it('maps a submit-time kernel error via logAndMapError on both paths', async () => {
const envelope = `__databricks_error__:${JSON.stringify({ code: 'SqlError', message: 'SUBMIT_BOOM' })}`;
for (const opts of [{}, { runAsync: true }]) {
const connection = new FakeNativeConnection();
connection.throwOnExecute = new Error(envelope); // fails executeStatementDirect / submitStatement
const session = makeSession(connection);
let thrown: unknown;
try {
// eslint-disable-next-line no-await-in-loop
await session.executeStatement('SELECT 1', opts);
} catch (err) {
thrown = err;
}
expect(thrown, `path ${JSON.stringify(opts)}`).to.be.instanceOf(HiveDriverError);
expect((thrown as Error).message).to.match(/SUBMIT_BOOM/);
}
});
// Genuinely unsupported on kernel — rejected (rather than silently ignored) so
// a caller/agent gets signal instead of a no-op. queryTags / queryTimeout /
// rowLimit are NOT here — they are forwarded (asserted above).
for (const { name, options, re } of [
{ name: 'useCloudFetch', options: { useCloudFetch: true }, re: /useCloudFetch/ },
{ name: 'useLZ4Compression', options: { useLZ4Compression: true }, re: /useLZ4Compression/ },
{ name: 'stagingAllowedLocalPath', options: { stagingAllowedLocalPath: '/tmp' }, re: /stagingAllowedLocalPath/ },
] as const) {
it(`executeStatement rejects ${name} rather than silently ignoring it`, async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
let thrown: unknown;
try {
await session.executeStatement('SELECT 1', options);
} catch (err) {
thrown = err;
}
expect(thrown).to.be.instanceOf(HiveDriverError);
expect((thrown as Error).message).to.match(re);
});
}
// Metadata calls forward to the kernel's metadata surface and wrap the
// returned napi `Statement` as a `KernelOperationBackend`. Each case asserts
// the request → napi-argument mapping (the only logic the driver owns).
it('metadata calls forward to the napi binding with mapped arguments', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
const op = await session.getCatalogs({});
expect(op).to.be.instanceOf(KernelOperationBackend);
await session.getSchemas({ catalogName: 'main', schemaName: 'def%' });
await session.getTables({ catalogName: 'main', schemaName: 'def', tableName: 't%', tableTypes: ['TABLE', 'VIEW'] });
await session.getColumns({ catalogName: 'main', schemaName: 'def', tableName: 't', columnName: 'c%' });
await session.getFunctions({ catalogName: 'main', schemaName: 'def', functionName: 'f%' });
await session.getTableTypes({});
await session.getTypeInfo({});
await session.getPrimaryKeys({ catalogName: 'main', schemaName: 'def', tableName: 't' });
await session.getCrossReference({
parentCatalogName: 'pc',
parentSchemaName: 'ps',
parentTableName: 'pt',
foreignCatalogName: 'fc',
foreignSchemaName: 'fs',
foreignTableName: 'ft',
});
expect(connection.metadataCalls).to.deep.equal([
['listCatalogs'],
['listSchemas', 'main', 'def%'],
['listTables', 'main', 'def', 't%', ['TABLE', 'VIEW']],
['listColumns', 'main', 'def', 't', 'c%'],
['listFunctions', 'main', 'def', 'f%'],
['listTableTypes'],
['listTypeInfo'],
['getPrimaryKeys', 'main', 'def', 't'],
['getCrossReference', 'pc', 'ps', 'pt', 'fc', 'fs', 'ft'],
]);
});
it('getPrimaryKeys rejects an omitted catalog up front (the kernel requires one)', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
for (const request of [
{ schemaName: 'def', tableName: 't' },
{ catalogName: '', schemaName: 'def', tableName: 't' },
]) {
let thrown: unknown;
try {
// eslint-disable-next-line no-await-in-loop
await session.getPrimaryKeys(request);
} catch (err) {
thrown = err;
}
expect(thrown, `expected reject for ${JSON.stringify(request)}`).to.be.instanceOf(HiveDriverError);
expect((thrown as Error).message).to.match(/requires a catalog/);
}
// The kernel call must NOT be reached (no empty-identifier sent over FFI).
expect(connection.metadataCalls.filter((c) => c[0] === 'getPrimaryKeys')).to.have.length(0);
});
it('getInfo synthesizes the three server-answered info types and rejects the rest', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
// CLI_DBMS_NAME (17) → "Spark SQL".
const info = await session.getInfo(17);
expect(info.getValue()).to.equal('Spark SQL');
// An unsupported info type (e.g. CLI_MAX_DRIVER_CONNECTIONS) is rejected,
// mirroring the Thrift server's reject-unsupported behaviour.
let thrown: unknown;
try {
await session.getInfo(0);
} catch (err) {
thrown = err;
}
expect(thrown).to.be.instanceOf(HiveDriverError);
});
it('close() forwards to the native connection', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
const status = await session.close();
expect(connection.closed).to.equal(true);
expect(status.isSuccess).to.equal(true);
});
it('close() is idempotent', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.close();
// Second call should not re-invoke connection.close
connection.closed = false;
const status = await session.close();
expect(connection.closed).to.equal(false);
expect(status.isSuccess).to.equal(true);
});
it('executeStatement fails after close()', async () => {
const connection = new FakeNativeConnection();
const session = makeSession(connection);
await session.close();
let thrown: unknown;
try {
await session.executeStatement('SELECT 1', {});
} catch (err) {
thrown = err;
}
expect(thrown).to.be.instanceOf(HiveDriverError);
});
});
describe('KernelOperationBackend', () => {
function makeOperation(statement: KernelStatement = new FakeNativeStatement()) {
return new KernelOperationBackend({ statement, context: makeContext() });
}
it('id is a stable string', () => {
const op = makeOperation();
expect(op.id).to.equal(op.id);
expect(op.id).to.be.a('string').and.have.length.greaterThan(0);
});
it('hasResultSet is true for M0', () => {
const op = makeOperation();
expect(op.hasResultSet()).to.equal(true);
});
it('cancel() forwards to napi Statement', async () => {
const stmt = new FakeNativeStatement();
const op = makeOperation(stmt);
await op.cancel();
expect(stmt.cancelled).to.equal(true);
});
it('cancel() is idempotent', async () => {
const stmt = new FakeNativeStatement();
const op = makeOperation(stmt);
await op.cancel();
stmt.cancelled = false;
await op.cancel();
expect(stmt.cancelled).to.equal(false);
});
it('close() forwards to napi Statement', async () => {
const stmt = new FakeNativeStatement();
const op = makeOperation(stmt);
await op.close();
expect(stmt.closed).to.equal(true);
});