-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathSeaSessionBackend.ts
More file actions
358 lines (334 loc) · 13.4 KB
/
Copy pathSeaSessionBackend.ts
File metadata and controls
358 lines (334 loc) · 13.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
// 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 { v4 as uuidv4 } from 'uuid';
import ISessionBackend from '../contracts/ISessionBackend';
import IOperationBackend from '../contracts/IOperationBackend';
import IClientContext from '../contracts/IClientContext';
import {
ExecuteStatementOptions,
TypeInfoRequest,
CatalogsRequest,
SchemasRequest,
TablesRequest,
TableTypesRequest,
ColumnsRequest,
FunctionsRequest,
PrimaryKeysRequest,
CrossReferenceRequest,
} from '../contracts/IDBSQLSession';
import Status from '../dto/Status';
import InfoValue from '../dto/InfoValue';
import HiveDriverError from '../errors/HiveDriverError';
import { SeaNativeConnection, SeaNativeExecuteOptions } from './SeaNativeLoader';
import { decodeNapiKernelError } from './SeaErrorMapping';
import SeaOperationBackend from './SeaOperationBackend';
import SeaTableTypeFilter from './SeaTableTypeFilter';
import { seaServerInfoValue } from './SeaServerInfo';
import { buildSeaPositionalParams, buildSeaNamedParams } from './SeaPositionalParams';
import ParameterError from '../errors/ParameterError';
import { emptyToUndefined, countParameterMarkers } from './SeaInputValidation';
export interface SeaSessionBackendOptions {
/** The opaque napi `Connection` handle returned by `openSession`. */
connection: SeaNativeConnection;
context: IClientContext;
/** Optional override for `id`. Defaults to a fresh UUIDv4. */
id?: string;
}
/**
* SEA-backed implementation of `ISessionBackend`.
*
* **M1 scope:** `executeStatement`, 9 of the 10 `IDBSQLSession`
* metadata methods, and `close`. The implemented nine are:
* `getTypeInfo`, `getCatalogs`, `getSchemas`, `getTables`,
* `getTableTypes`, `getColumns`, `getFunctions`, `getPrimaryKeys`,
* `getCrossReference`. `getInfo` is a stub-throw (deferred — the
* kernel `Metadata` API does not expose it yet); `getProcedures` is
* not on `IDBSQLSession` (see `SeaNativeLoader.ts` NOTE comment).
*
* All implemented metadata methods delegate directly to the corresponding
* napi `Connection` method — the kernel performs SHOW/information_schema
* queries and returns JDBC-shaped Arrow batches through a `Statement`
* handle identical to `executeStatement`. The JS layer wraps each
* returned `Statement` in a `SeaOperationBackend` so callers consume
* metadata results through the same `IOperationBackend` interface they
* use for SQL results.
*
* **Session config flow:** catalog / schema / sessionConf are applied
* once at session creation (kernel `Session::builder().defaults()` +
* `.session_conf()` → SEA `CreateSession.catalog` / `.schema` /
* `.session_confs`) and remain in effect for every statement run on
* the resulting napi `Connection`. No per-statement forwarding is
* needed — that pattern was removed when the napi binding moved these
* onto `openSession` to match pyo3.
*/
export default class SeaSessionBackend implements ISessionBackend {
private readonly connection: SeaNativeConnection;
private readonly context: IClientContext;
private readonly _id: string;
private closed = false;
constructor({ connection, context, id }: SeaSessionBackendOptions) {
this.connection = connection;
this.context = context;
this._id = id ?? uuidv4();
}
public get id(): string {
return this._id;
}
public async getInfo(infoType: number): Promise<InfoValue> {
this.failIfClosed();
// `getInfo` (TGetInfoReq) is a Thrift/JDBC concept with no SEA-protocol or
// kernel equivalent, so — like JDBC's DatabaseMetaData — we synthesize the
// values client-side. `seaServerInfoValue` returns matches for the three
// TGetInfoTypes the Thrift server answers (server name, DBMS name, DBMS
// version) and `undefined` for the rest, which we surface as an error to
// mirror the server's reject-unsupported-info-type behaviour.
const value = seaServerInfoValue(infoType);
if (value === undefined) {
throw new HiveDriverError(
`SEA getInfo: TGetInfoType ${infoType} is not supported. The SEA/kernel protocol ` +
'has no getInfo RPC; only CLI_SERVER_NAME, CLI_DBMS_NAME and CLI_DBMS_VER are ' +
'synthesised (matching the Thrift server, which also rejects all other info types).',
);
}
return new InfoValue(value);
}
/**
* Execute a SQL statement through the napi binding.
*
* Catalog / schema / sessionConf were applied at session open, so
* there are no per-statement options to thread through.
*
* M0 intentionally rejects `queryTimeout`, `namedParameters`, and
* `ordinalParameters` with explicit deferred-to-M1 errors. `useCloudFetch`
* is a no-op on the SEA path — the kernel hardcodes the SEA
* `disposition` to `INLINE_OR_EXTERNAL_LINKS`, and per-statement
* conf overrides have no reader on the kernel; cloud-fetch behaviour
* is governed entirely by the kernel's `ResultConfig` (M1 binding
* surface).
*
* The Thrift backend remains the path for consumers that need any
* of those today.
*/
public async executeStatement(statement: string, options: ExecuteStatementOptions): Promise<IOperationBackend> {
this.failIfClosed();
// Reduce `?` / `:name` bindings to the napi inputs the kernel param codec
// accepts (DECIMAL → DECIMAL(p,s), NULL → value-less), reusing
// DBSQLParameter's stringification. Positional and named are mutually
// exclusive at the SQL level (matches the Thrift backend).
const positionalParams = buildSeaPositionalParams(options.ordinalParameters);
const namedParams = buildSeaNamedParams(options.namedParameters);
if (positionalParams !== undefined && namedParams !== undefined) {
throw new ParameterError('Driver does not support both ordinal and named parameters.');
}
// Arity check: positional params must match the `?` marker count, or the
// server silently binds the prefix and drops the rest (data-correctness
// footgun). Markers inside string literals / comments are not counted.
if (positionalParams !== undefined) {
const markerCount = countParameterMarkers(statement);
if (positionalParams.length !== markerCount) {
throw new ParameterError(
`ordinalParameters length ${positionalParams.length} does not match the ` +
`${markerCount} '?' placeholder(s) in the SQL`,
);
}
}
const nativeOptions: SeaNativeExecuteOptions = {};
if (positionalParams !== undefined) {
nativeOptions.positionalParams = positionalParams;
}
if (namedParams !== undefined) {
nativeOptions.namedParams = namedParams;
}
// JDBC `setQueryTimeout` is whole seconds; the kernel's
// `query_timeout_secs` (SEA wait timeout, on_wait_timeout = CANCEL) is
// the native equivalent. The SEA wire caps it at 50s server-side.
if (options.queryTimeout !== undefined) {
nativeOptions.queryTimeoutSecs = Number(options.queryTimeout);
}
const hasOptions = Object.keys(nativeOptions).length > 0;
let nativeStatement;
try {
nativeStatement = hasOptions
? await this.connection.executeStatement(statement, nativeOptions)
: await this.connection.executeStatement(statement);
} catch (err) {
throw decodeNapiKernelError(err);
}
return new SeaOperationBackend({
statement: nativeStatement,
context: this.context,
});
}
public async getTypeInfo(_request: TypeInfoRequest): Promise<IOperationBackend> {
this.failIfClosed();
let nativeStatement;
try {
nativeStatement = await this.connection.listTypeInfo();
} catch (err) {
throw decodeNapiKernelError(err);
}
return new SeaOperationBackend({ statement: nativeStatement, context: this.context });
}
public async getCatalogs(_request: CatalogsRequest): Promise<IOperationBackend> {
this.failIfClosed();
let nativeStatement;
try {
nativeStatement = await this.connection.listCatalogs();
} catch (err) {
throw decodeNapiKernelError(err);
}
return new SeaOperationBackend({ statement: nativeStatement, context: this.context });
}
public async getSchemas(request: SchemasRequest): Promise<IOperationBackend> {
this.failIfClosed();
let nativeStatement;
try {
nativeStatement = await this.connection.listSchemas(
emptyToUndefined(request.catalogName),
emptyToUndefined(request.schemaName),
);
} catch (err) {
throw decodeNapiKernelError(err);
}
return new SeaOperationBackend({ statement: nativeStatement, context: this.context });
}
public async getTables(request: TablesRequest): Promise<IOperationBackend> {
this.failIfClosed();
let nativeStatement;
try {
nativeStatement = await this.connection.listTables(
emptyToUndefined(request.catalogName),
emptyToUndefined(request.schemaName),
emptyToUndefined(request.tableName),
request.tableTypes,
);
} catch (err) {
throw decodeNapiKernelError(err);
}
const backend = new SeaOperationBackend({ statement: nativeStatement, context: this.context });
// The server does not honour tableTypes server-side (advisory only).
// Apply client-side filter when the caller supplied a non-null list.
if (request.tableTypes != null) {
return new SeaTableTypeFilter(backend, new Set(request.tableTypes));
}
return backend;
}
public async getTableTypes(_request: TableTypesRequest): Promise<IOperationBackend> {
this.failIfClosed();
let nativeStatement;
try {
nativeStatement = await this.connection.listTableTypes();
} catch (err) {
throw decodeNapiKernelError(err);
}
return new SeaOperationBackend({ statement: nativeStatement, context: this.context });
}
public async getColumns(request: ColumnsRequest): Promise<IOperationBackend> {
this.failIfClosed();
let nativeStatement;
try {
nativeStatement = await this.connection.listColumns(
emptyToUndefined(request.catalogName),
emptyToUndefined(request.schemaName),
emptyToUndefined(request.tableName),
emptyToUndefined(request.columnName),
);
} catch (err) {
throw decodeNapiKernelError(err);
}
return new SeaOperationBackend({ statement: nativeStatement, context: this.context });
}
public async getFunctions(request: FunctionsRequest): Promise<IOperationBackend> {
this.failIfClosed();
let nativeStatement;
try {
nativeStatement = await this.connection.listFunctions(
emptyToUndefined(request.catalogName),
emptyToUndefined(request.schemaName),
emptyToUndefined(request.functionName),
);
} catch (err) {
throw decodeNapiKernelError(err);
}
return new SeaOperationBackend({ statement: nativeStatement, context: this.context });
}
public async getPrimaryKeys(request: PrimaryKeysRequest): Promise<IOperationBackend> {
this.failIfClosed();
if (!request.catalogName) {
throw new HiveDriverError(
'SeaSessionBackend.getPrimaryKeys: catalogName is required on the SEA path (kernel rejects empty identifiers; Thrift backend silently resolves to session default)',
);
}
let nativeStatement;
try {
nativeStatement = await this.connection.getPrimaryKeys(
request.catalogName,
request.schemaName,
request.tableName,
);
} catch (err) {
throw decodeNapiKernelError(err);
}
return new SeaOperationBackend({ statement: nativeStatement, context: this.context });
}
public async getCrossReference(request: CrossReferenceRequest): Promise<IOperationBackend> {
this.failIfClosed();
if (!request.foreignCatalogName) {
throw new HiveDriverError(
'SeaSessionBackend.getCrossReference: foreignCatalogName is required on the SEA path (kernel rejects empty identifiers)',
);
}
if (!request.foreignSchemaName) {
throw new HiveDriverError(
'SeaSessionBackend.getCrossReference: foreignSchemaName is required on the SEA path',
);
}
if (!request.foreignTableName) {
throw new HiveDriverError(
'SeaSessionBackend.getCrossReference: foreignTableName is required on the SEA path',
);
}
let nativeStatement;
try {
nativeStatement = await this.connection.getCrossReference(
request.parentCatalogName,
request.parentSchemaName,
request.parentTableName,
request.foreignCatalogName,
request.foreignSchemaName,
request.foreignTableName,
);
} catch (err) {
throw decodeNapiKernelError(err);
}
return new SeaOperationBackend({ statement: nativeStatement, context: this.context });
}
public async close(): Promise<Status> {
if (this.closed) {
return Status.success();
}
try {
await this.connection.close();
} catch (err) {
throw decodeNapiKernelError(err);
}
this.closed = true;
return Status.success();
}
private failIfClosed(): void {
if (this.closed) {
throw new HiveDriverError('SeaSessionBackend: session is closed');
}
}
}