@databricks/sql 1.8.3 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/README.md +4 -8
  2. package/dist/DBSQLClient.d.ts +8 -5
  3. package/dist/DBSQLClient.js +70 -82
  4. package/dist/DBSQLClient.js.map +1 -1
  5. package/dist/DBSQLLogger.js +2 -13
  6. package/dist/DBSQLLogger.js.map +1 -1
  7. package/dist/DBSQLOperation.d.ts +7 -2
  8. package/dist/DBSQLOperation.js +227 -237
  9. package/dist/DBSQLOperation.js.map +1 -1
  10. package/dist/DBSQLSession.d.ts +3 -1
  11. package/dist/DBSQLSession.js +284 -220
  12. package/dist/DBSQLSession.js.map +1 -1
  13. package/dist/connection/auth/DatabricksOAuth/AuthorizationCode.d.ts +7 -2
  14. package/dist/connection/auth/DatabricksOAuth/AuthorizationCode.js +91 -104
  15. package/dist/connection/auth/DatabricksOAuth/AuthorizationCode.js.map +1 -1
  16. package/dist/connection/auth/DatabricksOAuth/OAuthManager.js +114 -134
  17. package/dist/connection/auth/DatabricksOAuth/OAuthManager.js.map +1 -1
  18. package/dist/connection/auth/DatabricksOAuth/OAuthPersistence.js +4 -17
  19. package/dist/connection/auth/DatabricksOAuth/OAuthPersistence.js.map +1 -1
  20. package/dist/connection/auth/DatabricksOAuth/index.d.ts +3 -2
  21. package/dist/connection/auth/DatabricksOAuth/index.js +19 -22
  22. package/dist/connection/auth/DatabricksOAuth/index.js.map +1 -1
  23. package/dist/connection/auth/PlainHttpAuthentication.js +5 -13
  24. package/dist/connection/auth/PlainHttpAuthentication.js.map +1 -1
  25. package/dist/connection/connections/HttpConnection.d.ts +1 -1
  26. package/dist/connection/connections/HttpConnection.js +49 -47
  27. package/dist/connection/connections/HttpConnection.js.map +1 -1
  28. package/dist/connection/connections/HttpRetryPolicy.d.ts +1 -1
  29. package/dist/connection/connections/HttpRetryPolicy.js +28 -41
  30. package/dist/connection/connections/HttpRetryPolicy.js.map +1 -1
  31. package/dist/connection/connections/NullRetryPolicy.js +5 -18
  32. package/dist/connection/connections/NullRetryPolicy.js.map +1 -1
  33. package/dist/connection/connections/ThriftHttpConnection.js +22 -20
  34. package/dist/connection/connections/ThriftHttpConnection.js.map +1 -1
  35. package/dist/connection/contracts/IConnectionProvider.d.ts +1 -1
  36. package/dist/contracts/IClientContext.d.ts +4 -2
  37. package/dist/contracts/IDBSQLSession.d.ts +21 -13
  38. package/dist/contracts/IOperation.d.ts +23 -0
  39. package/dist/contracts/IThriftClient.d.ts +8 -0
  40. package/dist/contracts/IThriftClient.js +3 -0
  41. package/dist/contracts/IThriftClient.js.map +1 -0
  42. package/dist/dto/InfoValue.d.ts +1 -2
  43. package/dist/hive/Commands/BaseCommand.d.ts +3 -4
  44. package/dist/hive/Commands/BaseCommand.js +22 -33
  45. package/dist/hive/Commands/BaseCommand.js.map +1 -1
  46. package/dist/hive/Commands/CancelDelegationTokenCommand.d.ts +4 -1
  47. package/dist/hive/Commands/CancelDelegationTokenCommand.js.map +1 -1
  48. package/dist/hive/Commands/CancelOperationCommand.d.ts +4 -1
  49. package/dist/hive/Commands/CancelOperationCommand.js.map +1 -1
  50. package/dist/hive/Commands/CloseOperationCommand.d.ts +4 -1
  51. package/dist/hive/Commands/CloseOperationCommand.js.map +1 -1
  52. package/dist/hive/Commands/CloseSessionCommand.d.ts +4 -1
  53. package/dist/hive/Commands/CloseSessionCommand.js.map +1 -1
  54. package/dist/hive/Commands/ExecuteStatementCommand.d.ts +4 -1
  55. package/dist/hive/Commands/ExecuteStatementCommand.js.map +1 -1
  56. package/dist/hive/Commands/FetchResultsCommand.d.ts +4 -1
  57. package/dist/hive/Commands/FetchResultsCommand.js.map +1 -1
  58. package/dist/hive/Commands/GetCatalogsCommand.d.ts +4 -1
  59. package/dist/hive/Commands/GetCatalogsCommand.js.map +1 -1
  60. package/dist/hive/Commands/GetColumnsCommand.d.ts +4 -1
  61. package/dist/hive/Commands/GetColumnsCommand.js.map +1 -1
  62. package/dist/hive/Commands/GetCrossReferenceCommand.d.ts +4 -1
  63. package/dist/hive/Commands/GetCrossReferenceCommand.js.map +1 -1
  64. package/dist/hive/Commands/GetDelegationTokenCommand.d.ts +4 -1
  65. package/dist/hive/Commands/GetDelegationTokenCommand.js.map +1 -1
  66. package/dist/hive/Commands/GetFunctionsCommand.d.ts +4 -1
  67. package/dist/hive/Commands/GetFunctionsCommand.js.map +1 -1
  68. package/dist/hive/Commands/GetInfoCommand.d.ts +4 -1
  69. package/dist/hive/Commands/GetInfoCommand.js.map +1 -1
  70. package/dist/hive/Commands/GetOperationStatusCommand.d.ts +4 -1
  71. package/dist/hive/Commands/GetOperationStatusCommand.js.map +1 -1
  72. package/dist/hive/Commands/GetPrimaryKeysCommand.d.ts +4 -1
  73. package/dist/hive/Commands/GetPrimaryKeysCommand.js.map +1 -1
  74. package/dist/hive/Commands/GetResultSetMetadataCommand.d.ts +4 -1
  75. package/dist/hive/Commands/GetResultSetMetadataCommand.js.map +1 -1
  76. package/dist/hive/Commands/GetSchemasCommand.d.ts +4 -1
  77. package/dist/hive/Commands/GetSchemasCommand.js.map +1 -1
  78. package/dist/hive/Commands/GetTableTypesCommand.d.ts +4 -1
  79. package/dist/hive/Commands/GetTableTypesCommand.js.map +1 -1
  80. package/dist/hive/Commands/GetTablesCommand.d.ts +4 -1
  81. package/dist/hive/Commands/GetTablesCommand.js.map +1 -1
  82. package/dist/hive/Commands/GetTypeInfoCommand.d.ts +4 -1
  83. package/dist/hive/Commands/GetTypeInfoCommand.js.map +1 -1
  84. package/dist/hive/Commands/OpenSessionCommand.d.ts +4 -1
  85. package/dist/hive/Commands/OpenSessionCommand.js.map +1 -1
  86. package/dist/hive/Commands/RenewDelegationTokenCommand.d.ts +4 -1
  87. package/dist/hive/Commands/RenewDelegationTokenCommand.js.map +1 -1
  88. package/dist/hive/HiveDriver.js +104 -155
  89. package/dist/hive/HiveDriver.js.map +1 -1
  90. package/dist/polyfills.d.ts +1 -1
  91. package/dist/polyfills.js +3 -2
  92. package/dist/polyfills.js.map +1 -1
  93. package/dist/result/ArrowResultConverter.d.ts +1 -1
  94. package/dist/result/ArrowResultConverter.js +69 -84
  95. package/dist/result/ArrowResultConverter.js.map +1 -1
  96. package/dist/result/ArrowResultHandler.d.ts +1 -1
  97. package/dist/result/ArrowResultHandler.js +33 -42
  98. package/dist/result/ArrowResultHandler.js.map +1 -1
  99. package/dist/result/CloudFetchResultHandler.d.ts +1 -1
  100. package/dist/result/CloudFetchResultHandler.js +53 -66
  101. package/dist/result/CloudFetchResultHandler.js.map +1 -1
  102. package/dist/result/JsonResultHandler.d.ts +0 -1
  103. package/dist/result/JsonResultHandler.js +14 -41
  104. package/dist/result/JsonResultHandler.js.map +1 -1
  105. package/dist/result/ResultSlicer.js +40 -53
  106. package/dist/result/ResultSlicer.js.map +1 -1
  107. package/dist/result/RowSetProvider.d.ts +4 -0
  108. package/dist/result/RowSetProvider.js +42 -59
  109. package/dist/result/RowSetProvider.js.map +1 -1
  110. package/dist/result/utils.d.ts +2 -1
  111. package/dist/result/utils.js +9 -1
  112. package/dist/result/utils.js.map +1 -1
  113. package/dist/utils/CloseableCollection.js +7 -18
  114. package/dist/utils/CloseableCollection.js.map +1 -1
  115. package/dist/utils/OperationIterator.d.ts +23 -0
  116. package/dist/utils/OperationIterator.js +71 -0
  117. package/dist/utils/OperationIterator.js.map +1 -0
  118. package/dist/utils/index.d.ts +2 -1
  119. package/dist/utils/index.js +3 -1
  120. package/dist/utils/index.js.map +1 -1
  121. package/dist/utils/lz4.d.ts +3 -0
  122. package/dist/utils/lz4.js +15 -0
  123. package/dist/utils/lz4.js.map +1 -0
  124. package/dist/version.d.ts +1 -1
  125. package/dist/version.js +1 -1
  126. package/package.json +15 -8
  127. package/thrift/TCLIService.d.ts +0 -42
  128. package/dist/hive/Types/index.d.ts +0 -18
  129. package/dist/hive/Types/index.js +0 -25
  130. package/dist/hive/Types/index.js.map +0 -1
@@ -22,24 +22,18 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
35
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
27
  };
37
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.numberToInt64 = void 0;
38
30
  const fs = __importStar(require("fs"));
39
31
  const path = __importStar(require("path"));
32
+ const node_stream_1 = __importDefault(require("node:stream"));
33
+ const node_util_1 = __importDefault(require("node:util"));
40
34
  const uuid_1 = require("uuid");
35
+ const node_int64_1 = __importDefault(require("node-int64"));
41
36
  const node_fetch_1 = __importDefault(require("node-fetch"));
42
- const Types_1 = require("./hive/Types");
43
37
  const DBSQLOperation_1 = __importDefault(require("./DBSQLOperation"));
44
38
  const Status_1 = __importDefault(require("./dto/Status"));
45
39
  const InfoValue_1 = __importDefault(require("./dto/InfoValue"));
@@ -50,14 +44,28 @@ const HiveDriverError_1 = __importDefault(require("./errors/HiveDriverError"));
50
44
  const StagingError_1 = __importDefault(require("./errors/StagingError"));
51
45
  const DBSQLParameter_1 = require("./DBSQLParameter");
52
46
  const ParameterError_1 = __importDefault(require("./errors/ParameterError"));
53
- const defaultMaxRows = 100000;
54
- function getDirectResultsOptions(maxRows = defaultMaxRows) {
47
+ // Explicitly promisify a callback-style `pipeline` because `node:stream/promises` is not available in Node 14
48
+ const pipeline = node_util_1.default.promisify(node_stream_1.default.pipeline);
49
+ function numberToInt64(value) {
50
+ if (value instanceof node_int64_1.default) {
51
+ return value;
52
+ }
53
+ if (typeof value === 'bigint') {
54
+ const buffer = new ArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT);
55
+ const view = new DataView(buffer);
56
+ view.setBigInt64(0, value, false); // `false` to use big-endian order
57
+ return new node_int64_1.default(Buffer.from(buffer));
58
+ }
59
+ return new node_int64_1.default(value);
60
+ }
61
+ exports.numberToInt64 = numberToInt64;
62
+ function getDirectResultsOptions(maxRows, config) {
55
63
  if (maxRows === null) {
56
64
  return {};
57
65
  }
58
66
  return {
59
67
  getDirectResults: {
60
- maxRows: new Types_1.Int64(maxRows),
68
+ maxRows: numberToInt64(maxRows !== null && maxRows !== void 0 ? maxRows : config.directResultsDefaultMaxRows),
61
69
  },
62
70
  };
63
71
  }
@@ -79,7 +87,7 @@ function getArrowOptions(config) {
79
87
  },
80
88
  };
81
89
  }
82
- function getQueryParameters(sessionHandle, namedParameters, ordinalParameters) {
90
+ function getQueryParameters(namedParameters, ordinalParameters) {
83
91
  const namedParametersProvided = namedParameters !== undefined && Object.keys(namedParameters).length > 0;
84
92
  const ordinalParametersProvided = ordinalParameters !== undefined && ordinalParameters.length > 0;
85
93
  if (namedParametersProvided && ordinalParametersProvided) {
@@ -110,11 +118,12 @@ class DBSQLSession {
110
118
  this.operations = new CloseableCollection_1.default();
111
119
  this.sessionHandle = handle;
112
120
  this.context = context;
113
- this.context.getLogger().log(IDBSQLLogger_1.LogLevel.debug, `Session created with id: ${this.getId()}`);
121
+ this.context.getLogger().log(IDBSQLLogger_1.LogLevel.debug, `Session created with id: ${this.id}`);
114
122
  }
115
- getId() {
123
+ get id() {
116
124
  var _a, _b;
117
- return (0, uuid_1.stringify)(((_b = (_a = this.sessionHandle) === null || _a === void 0 ? void 0 : _a.sessionId) === null || _b === void 0 ? void 0 : _b.guid) || (0, uuid_1.parse)(uuid_1.NIL));
125
+ const sessionId = (_b = (_a = this.sessionHandle) === null || _a === void 0 ? void 0 : _a.sessionId) === null || _b === void 0 ? void 0 : _b.guid;
126
+ return sessionId ? (0, uuid_1.stringify)(sessionId) : uuid_1.NIL;
118
127
  }
119
128
  /**
120
129
  * Fetches info
@@ -124,18 +133,16 @@ class DBSQLSession {
124
133
  * @example
125
134
  * const response = await session.getInfo(thrift.TCLIService_types.TGetInfoType.CLI_DBMS_VER);
126
135
  */
127
- getInfo(infoType) {
128
- return __awaiter(this, void 0, void 0, function* () {
129
- yield this.failIfClosed();
130
- const driver = yield this.context.getDriver();
131
- const operationPromise = driver.getInfo({
132
- sessionHandle: this.sessionHandle,
133
- infoType,
134
- });
135
- const response = yield this.handleResponse(operationPromise);
136
- Status_1.default.assert(response.status);
137
- return new InfoValue_1.default(response.infoValue);
136
+ async getInfo(infoType) {
137
+ await this.failIfClosed();
138
+ const driver = await this.context.getDriver();
139
+ const operationPromise = driver.getInfo({
140
+ sessionHandle: this.sessionHandle,
141
+ infoType,
138
142
  });
143
+ const response = await this.handleResponse(operationPromise);
144
+ Status_1.default.assert(response.status);
145
+ return new InfoValue_1.default(response.infoValue);
139
146
  }
140
147
  /**
141
148
  * Executes statement
@@ -146,112 +153,126 @@ class DBSQLSession {
146
153
  * @example
147
154
  * const operation = await session.executeStatement(query);
148
155
  */
149
- executeStatement(statement, options = {}) {
156
+ async executeStatement(statement, options = {}) {
150
157
  var _a;
151
- return __awaiter(this, void 0, void 0, function* () {
152
- yield this.failIfClosed();
153
- const driver = yield this.context.getDriver();
154
- const clientConfig = this.context.getConfig();
155
- const operationPromise = driver.executeStatement(Object.assign(Object.assign(Object.assign({ sessionHandle: this.sessionHandle, statement, queryTimeout: options.queryTimeout, runAsync: true }, getDirectResultsOptions(options.maxRows)), getArrowOptions(clientConfig)), { canDownloadResult: (_a = options.useCloudFetch) !== null && _a !== void 0 ? _a : clientConfig.useCloudFetch, parameters: getQueryParameters(this.sessionHandle, options.namedParameters, options.ordinalParameters), canDecompressLZ4Result: clientConfig.useLZ4Compression }));
156
- const response = yield this.handleResponse(operationPromise);
157
- const operation = this.createOperation(response);
158
- // If `stagingAllowedLocalPath` is provided - assume that operation possibly may be a staging operation.
159
- // To know for sure, fetch metadata and check a `isStagingOperation` flag. If it happens that it wasn't
160
- // a staging operation - not a big deal, we just fetched metadata earlier, but operation is still usable
161
- // and user can get data from it.
162
- // If `stagingAllowedLocalPath` is not provided - don't do anything to the operation. In a case of regular
163
- // operation, everything will work as usual. In a case of staging operation, it will be processed like any
164
- // other query - it will be possible to get data from it as usual, or use other operation methods.
165
- if (options.stagingAllowedLocalPath !== undefined) {
166
- const metadata = yield operation.getMetadata();
167
- if (metadata.isStagingOperation) {
168
- const allowedLocalPath = Array.isArray(options.stagingAllowedLocalPath)
169
- ? options.stagingAllowedLocalPath
170
- : [options.stagingAllowedLocalPath];
171
- return this.handleStagingOperation(operation, allowedLocalPath);
172
- }
173
- }
174
- return operation;
158
+ await this.failIfClosed();
159
+ const driver = await this.context.getDriver();
160
+ const clientConfig = this.context.getConfig();
161
+ const operationPromise = driver.executeStatement({
162
+ sessionHandle: this.sessionHandle,
163
+ statement,
164
+ queryTimeout: options.queryTimeout ? numberToInt64(options.queryTimeout) : undefined,
165
+ runAsync: true,
166
+ ...getDirectResultsOptions(options.maxRows, clientConfig),
167
+ ...getArrowOptions(clientConfig),
168
+ canDownloadResult: (_a = options.useCloudFetch) !== null && _a !== void 0 ? _a : clientConfig.useCloudFetch,
169
+ parameters: getQueryParameters(options.namedParameters, options.ordinalParameters),
170
+ canDecompressLZ4Result: clientConfig.useLZ4Compression && Boolean(utils_1.LZ4),
175
171
  });
176
- }
177
- handleStagingOperation(operation, allowedLocalPath) {
178
- return __awaiter(this, void 0, void 0, function* () {
179
- const rows = yield operation.fetchAll();
180
- if (rows.length !== 1) {
181
- throw new StagingError_1.default('Staging operation: expected only one row in result');
172
+ const response = await this.handleResponse(operationPromise);
173
+ const operation = this.createOperation(response);
174
+ // If `stagingAllowedLocalPath` is provided - assume that operation possibly may be a staging operation.
175
+ // To know for sure, fetch metadata and check a `isStagingOperation` flag. If it happens that it wasn't
176
+ // a staging operation - not a big deal, we just fetched metadata earlier, but operation is still usable
177
+ // and user can get data from it.
178
+ // If `stagingAllowedLocalPath` is not provided - don't do anything to the operation. In a case of regular
179
+ // operation, everything will work as usual. In a case of staging operation, it will be processed like any
180
+ // other query - it will be possible to get data from it as usual, or use other operation methods.
181
+ if (options.stagingAllowedLocalPath !== undefined) {
182
+ const metadata = await operation.getMetadata();
183
+ if (metadata.isStagingOperation) {
184
+ const allowedLocalPath = Array.isArray(options.stagingAllowedLocalPath)
185
+ ? options.stagingAllowedLocalPath
186
+ : [options.stagingAllowedLocalPath];
187
+ return this.handleStagingOperation(operation, allowedLocalPath);
182
188
  }
183
- const row = rows[0];
184
- // For REMOVE operation local file is not available, so no need to validate it
185
- if (row.localFile !== undefined) {
186
- let allowOperation = false;
187
- for (const filepath of allowedLocalPath) {
188
- const relativePath = path.relative(filepath, row.localFile);
189
- if (!relativePath.startsWith('..') && !path.isAbsolute(relativePath)) {
190
- allowOperation = true;
191
- }
192
- }
193
- if (!allowOperation) {
194
- throw new StagingError_1.default('Staging path not a subset of allowed local paths.');
189
+ }
190
+ return operation;
191
+ }
192
+ async handleStagingOperation(operation, allowedLocalPath) {
193
+ const rows = await operation.fetchAll();
194
+ if (rows.length !== 1) {
195
+ throw new StagingError_1.default('Staging operation: expected only one row in result');
196
+ }
197
+ const row = rows[0];
198
+ // For REMOVE operation local file is not available, so no need to validate it
199
+ if (row.localFile !== undefined) {
200
+ let allowOperation = false;
201
+ for (const filepath of allowedLocalPath) {
202
+ const relativePath = path.relative(filepath, row.localFile);
203
+ if (!relativePath.startsWith('..') && !path.isAbsolute(relativePath)) {
204
+ allowOperation = true;
195
205
  }
196
206
  }
197
- const { localFile, presignedUrl, headers } = row;
198
- switch (row.operation) {
199
- case 'GET':
200
- yield this.handleStagingGet(localFile, presignedUrl, headers);
201
- return operation;
202
- case 'PUT':
203
- yield this.handleStagingPut(localFile, presignedUrl, headers);
204
- return operation;
205
- case 'REMOVE':
206
- yield this.handleStagingRemove(presignedUrl, headers);
207
- return operation;
208
- default:
209
- throw new StagingError_1.default(`Staging query operation is not supported: ${row.operation}`);
207
+ if (!allowOperation) {
208
+ throw new StagingError_1.default('Staging path not a subset of allowed local paths.');
210
209
  }
211
- });
210
+ }
211
+ const { localFile, presignedUrl, headers } = row;
212
+ switch (row.operation) {
213
+ case 'GET':
214
+ await this.handleStagingGet(localFile, presignedUrl, headers);
215
+ return operation;
216
+ case 'PUT':
217
+ await this.handleStagingPut(localFile, presignedUrl, headers);
218
+ return operation;
219
+ case 'REMOVE':
220
+ await this.handleStagingRemove(presignedUrl, headers);
221
+ return operation;
222
+ default:
223
+ throw new StagingError_1.default(`Staging query operation is not supported: ${row.operation}`);
224
+ }
212
225
  }
213
- handleStagingGet(localFile, presignedUrl, headers) {
214
- return __awaiter(this, void 0, void 0, function* () {
215
- if (localFile === undefined) {
216
- throw new StagingError_1.default('Local file path not provided');
217
- }
218
- const connectionProvider = yield this.context.getConnectionProvider();
219
- const agent = yield connectionProvider.getAgent();
220
- const response = yield (0, node_fetch_1.default)(presignedUrl, { method: 'GET', headers, agent });
221
- if (!response.ok) {
222
- throw new StagingError_1.default(`HTTP error ${response.status} ${response.statusText}`);
223
- }
224
- const buffer = yield response.arrayBuffer();
225
- fs.writeFileSync(localFile, Buffer.from(buffer));
226
- });
226
+ async handleStagingGet(localFile, presignedUrl, headers) {
227
+ if (localFile === undefined) {
228
+ throw new StagingError_1.default('Local file path not provided');
229
+ }
230
+ const connectionProvider = await this.context.getConnectionProvider();
231
+ const agent = await connectionProvider.getAgent();
232
+ const response = await (0, node_fetch_1.default)(presignedUrl, { method: 'GET', headers, agent });
233
+ if (!response.ok) {
234
+ throw new StagingError_1.default(`HTTP error ${response.status} ${response.statusText}`);
235
+ }
236
+ const fileStream = fs.createWriteStream(localFile);
237
+ // `pipeline` will do all the dirty job for us, including error handling and closing all the streams properly
238
+ return pipeline(response.body, fileStream);
227
239
  }
228
- handleStagingRemove(presignedUrl, headers) {
229
- return __awaiter(this, void 0, void 0, function* () {
230
- const connectionProvider = yield this.context.getConnectionProvider();
231
- const agent = yield connectionProvider.getAgent();
232
- const response = yield (0, node_fetch_1.default)(presignedUrl, { method: 'DELETE', headers, agent });
233
- // Looks that AWS and Azure have a different behavior of HTTP `DELETE` for non-existing files
234
- // AWS assumes that - since file already doesn't exist - the goal is achieved, and returns HTTP 200
235
- // Azure, on the other hand, is somewhat stricter and check if file exists before deleting it. And if
236
- // file doesn't exist - Azure returns HTTP 404
237
- if (!response.ok) {
238
- throw new StagingError_1.default(`HTTP error ${response.status} ${response.statusText}`);
239
- }
240
- });
240
+ async handleStagingRemove(presignedUrl, headers) {
241
+ const connectionProvider = await this.context.getConnectionProvider();
242
+ const agent = await connectionProvider.getAgent();
243
+ const response = await (0, node_fetch_1.default)(presignedUrl, { method: 'DELETE', headers, agent });
244
+ // Looks that AWS and Azure have a different behavior of HTTP `DELETE` for non-existing files.
245
+ // AWS assumes that - since file already doesn't exist - the goal is achieved, and returns HTTP 200.
246
+ // Azure, on the other hand, is somewhat stricter and check if file exists before deleting it. And if
247
+ // file doesn't exist - Azure returns HTTP 404.
248
+ //
249
+ // For us, it's totally okay if file didn't exist before removing. So when we get an HTTP 404 -
250
+ // just ignore it and report success. This way we can have a uniform library behavior for all clouds
251
+ if (!response.ok && response.status !== 404) {
252
+ throw new StagingError_1.default(`HTTP error ${response.status} ${response.statusText}`);
253
+ }
241
254
  }
242
- handleStagingPut(localFile, presignedUrl, headers) {
243
- return __awaiter(this, void 0, void 0, function* () {
244
- if (localFile === undefined) {
245
- throw new StagingError_1.default('Local file path not provided');
246
- }
247
- const connectionProvider = yield this.context.getConnectionProvider();
248
- const agent = yield connectionProvider.getAgent();
249
- const data = fs.readFileSync(localFile);
250
- const response = yield (0, node_fetch_1.default)(presignedUrl, { method: 'PUT', headers, agent, body: data });
251
- if (!response.ok) {
252
- throw new StagingError_1.default(`HTTP error ${response.status} ${response.statusText}`);
253
- }
255
+ async handleStagingPut(localFile, presignedUrl, headers) {
256
+ if (localFile === undefined) {
257
+ throw new StagingError_1.default('Local file path not provided');
258
+ }
259
+ const connectionProvider = await this.context.getConnectionProvider();
260
+ const agent = await connectionProvider.getAgent();
261
+ const fileStream = fs.createReadStream(localFile);
262
+ const fileInfo = fs.statSync(localFile, { bigint: true });
263
+ const response = await (0, node_fetch_1.default)(presignedUrl, {
264
+ method: 'PUT',
265
+ headers: {
266
+ ...headers,
267
+ // This header is required by server
268
+ 'Content-Length': fileInfo.size.toString(),
269
+ },
270
+ agent,
271
+ body: fileStream,
254
272
  });
273
+ if (!response.ok) {
274
+ throw new StagingError_1.default(`HTTP error ${response.status} ${response.statusText}`);
275
+ }
255
276
  }
256
277
  /**
257
278
  * Information about supported data types
@@ -259,14 +280,17 @@ class DBSQLSession {
259
280
  * @param request
260
281
  * @returns DBSQLOperation
261
282
  */
262
- getTypeInfo(request = {}) {
263
- return __awaiter(this, void 0, void 0, function* () {
264
- yield this.failIfClosed();
265
- const driver = yield this.context.getDriver();
266
- const operationPromise = driver.getTypeInfo(Object.assign({ sessionHandle: this.sessionHandle, runAsync: true }, getDirectResultsOptions(request.maxRows)));
267
- const response = yield this.handleResponse(operationPromise);
268
- return this.createOperation(response);
283
+ async getTypeInfo(request = {}) {
284
+ await this.failIfClosed();
285
+ const driver = await this.context.getDriver();
286
+ const clientConfig = this.context.getConfig();
287
+ const operationPromise = driver.getTypeInfo({
288
+ sessionHandle: this.sessionHandle,
289
+ runAsync: true,
290
+ ...getDirectResultsOptions(request.maxRows, clientConfig),
269
291
  });
292
+ const response = await this.handleResponse(operationPromise);
293
+ return this.createOperation(response);
270
294
  }
271
295
  /**
272
296
  * Get list of catalogs
@@ -274,14 +298,17 @@ class DBSQLSession {
274
298
  * @param request
275
299
  * @returns DBSQLOperation
276
300
  */
277
- getCatalogs(request = {}) {
278
- return __awaiter(this, void 0, void 0, function* () {
279
- yield this.failIfClosed();
280
- const driver = yield this.context.getDriver();
281
- const operationPromise = driver.getCatalogs(Object.assign({ sessionHandle: this.sessionHandle, runAsync: true }, getDirectResultsOptions(request.maxRows)));
282
- const response = yield this.handleResponse(operationPromise);
283
- return this.createOperation(response);
301
+ async getCatalogs(request = {}) {
302
+ await this.failIfClosed();
303
+ const driver = await this.context.getDriver();
304
+ const clientConfig = this.context.getConfig();
305
+ const operationPromise = driver.getCatalogs({
306
+ sessionHandle: this.sessionHandle,
307
+ runAsync: true,
308
+ ...getDirectResultsOptions(request.maxRows, clientConfig),
284
309
  });
310
+ const response = await this.handleResponse(operationPromise);
311
+ return this.createOperation(response);
285
312
  }
286
313
  /**
287
314
  * Get list of schemas
@@ -289,14 +316,19 @@ class DBSQLSession {
289
316
  * @param request
290
317
  * @returns DBSQLOperation
291
318
  */
292
- getSchemas(request = {}) {
293
- return __awaiter(this, void 0, void 0, function* () {
294
- yield this.failIfClosed();
295
- const driver = yield this.context.getDriver();
296
- const operationPromise = driver.getSchemas(Object.assign({ sessionHandle: this.sessionHandle, catalogName: request.catalogName, schemaName: request.schemaName, runAsync: true }, getDirectResultsOptions(request.maxRows)));
297
- const response = yield this.handleResponse(operationPromise);
298
- return this.createOperation(response);
319
+ async getSchemas(request = {}) {
320
+ await this.failIfClosed();
321
+ const driver = await this.context.getDriver();
322
+ const clientConfig = this.context.getConfig();
323
+ const operationPromise = driver.getSchemas({
324
+ sessionHandle: this.sessionHandle,
325
+ catalogName: request.catalogName,
326
+ schemaName: request.schemaName,
327
+ runAsync: true,
328
+ ...getDirectResultsOptions(request.maxRows, clientConfig),
299
329
  });
330
+ const response = await this.handleResponse(operationPromise);
331
+ return this.createOperation(response);
300
332
  }
301
333
  /**
302
334
  * Get list of tables
@@ -304,14 +336,21 @@ class DBSQLSession {
304
336
  * @param request
305
337
  * @returns DBSQLOperation
306
338
  */
307
- getTables(request = {}) {
308
- return __awaiter(this, void 0, void 0, function* () {
309
- yield this.failIfClosed();
310
- const driver = yield this.context.getDriver();
311
- const operationPromise = driver.getTables(Object.assign({ sessionHandle: this.sessionHandle, catalogName: request.catalogName, schemaName: request.schemaName, tableName: request.tableName, tableTypes: request.tableTypes, runAsync: true }, getDirectResultsOptions(request.maxRows)));
312
- const response = yield this.handleResponse(operationPromise);
313
- return this.createOperation(response);
339
+ async getTables(request = {}) {
340
+ await this.failIfClosed();
341
+ const driver = await this.context.getDriver();
342
+ const clientConfig = this.context.getConfig();
343
+ const operationPromise = driver.getTables({
344
+ sessionHandle: this.sessionHandle,
345
+ catalogName: request.catalogName,
346
+ schemaName: request.schemaName,
347
+ tableName: request.tableName,
348
+ tableTypes: request.tableTypes,
349
+ runAsync: true,
350
+ ...getDirectResultsOptions(request.maxRows, clientConfig),
314
351
  });
352
+ const response = await this.handleResponse(operationPromise);
353
+ return this.createOperation(response);
315
354
  }
316
355
  /**
317
356
  * Get list of supported table types
@@ -319,14 +358,17 @@ class DBSQLSession {
319
358
  * @param request
320
359
  * @returns DBSQLOperation
321
360
  */
322
- getTableTypes(request = {}) {
323
- return __awaiter(this, void 0, void 0, function* () {
324
- yield this.failIfClosed();
325
- const driver = yield this.context.getDriver();
326
- const operationPromise = driver.getTableTypes(Object.assign({ sessionHandle: this.sessionHandle, runAsync: true }, getDirectResultsOptions(request.maxRows)));
327
- const response = yield this.handleResponse(operationPromise);
328
- return this.createOperation(response);
361
+ async getTableTypes(request = {}) {
362
+ await this.failIfClosed();
363
+ const driver = await this.context.getDriver();
364
+ const clientConfig = this.context.getConfig();
365
+ const operationPromise = driver.getTableTypes({
366
+ sessionHandle: this.sessionHandle,
367
+ runAsync: true,
368
+ ...getDirectResultsOptions(request.maxRows, clientConfig),
329
369
  });
370
+ const response = await this.handleResponse(operationPromise);
371
+ return this.createOperation(response);
330
372
  }
331
373
  /**
332
374
  * Get full information about columns of the table
@@ -334,14 +376,21 @@ class DBSQLSession {
334
376
  * @param request
335
377
  * @returns DBSQLOperation
336
378
  */
337
- getColumns(request = {}) {
338
- return __awaiter(this, void 0, void 0, function* () {
339
- yield this.failIfClosed();
340
- const driver = yield this.context.getDriver();
341
- const operationPromise = driver.getColumns(Object.assign({ sessionHandle: this.sessionHandle, catalogName: request.catalogName, schemaName: request.schemaName, tableName: request.tableName, columnName: request.columnName, runAsync: true }, getDirectResultsOptions(request.maxRows)));
342
- const response = yield this.handleResponse(operationPromise);
343
- return this.createOperation(response);
379
+ async getColumns(request = {}) {
380
+ await this.failIfClosed();
381
+ const driver = await this.context.getDriver();
382
+ const clientConfig = this.context.getConfig();
383
+ const operationPromise = driver.getColumns({
384
+ sessionHandle: this.sessionHandle,
385
+ catalogName: request.catalogName,
386
+ schemaName: request.schemaName,
387
+ tableName: request.tableName,
388
+ columnName: request.columnName,
389
+ runAsync: true,
390
+ ...getDirectResultsOptions(request.maxRows, clientConfig),
344
391
  });
392
+ const response = await this.handleResponse(operationPromise);
393
+ return this.createOperation(response);
345
394
  }
346
395
  /**
347
396
  * Get information about function
@@ -349,23 +398,35 @@ class DBSQLSession {
349
398
  * @param request
350
399
  * @returns DBSQLOperation
351
400
  */
352
- getFunctions(request) {
353
- return __awaiter(this, void 0, void 0, function* () {
354
- yield this.failIfClosed();
355
- const driver = yield this.context.getDriver();
356
- const operationPromise = driver.getFunctions(Object.assign({ sessionHandle: this.sessionHandle, catalogName: request.catalogName, schemaName: request.schemaName, functionName: request.functionName, runAsync: true }, getDirectResultsOptions(request.maxRows)));
357
- const response = yield this.handleResponse(operationPromise);
358
- return this.createOperation(response);
401
+ async getFunctions(request) {
402
+ await this.failIfClosed();
403
+ const driver = await this.context.getDriver();
404
+ const clientConfig = this.context.getConfig();
405
+ const operationPromise = driver.getFunctions({
406
+ sessionHandle: this.sessionHandle,
407
+ catalogName: request.catalogName,
408
+ schemaName: request.schemaName,
409
+ functionName: request.functionName,
410
+ runAsync: true,
411
+ ...getDirectResultsOptions(request.maxRows, clientConfig),
359
412
  });
413
+ const response = await this.handleResponse(operationPromise);
414
+ return this.createOperation(response);
360
415
  }
361
- getPrimaryKeys(request) {
362
- return __awaiter(this, void 0, void 0, function* () {
363
- yield this.failIfClosed();
364
- const driver = yield this.context.getDriver();
365
- const operationPromise = driver.getPrimaryKeys(Object.assign({ sessionHandle: this.sessionHandle, catalogName: request.catalogName, schemaName: request.schemaName, tableName: request.tableName, runAsync: true }, getDirectResultsOptions(request.maxRows)));
366
- const response = yield this.handleResponse(operationPromise);
367
- return this.createOperation(response);
416
+ async getPrimaryKeys(request) {
417
+ await this.failIfClosed();
418
+ const driver = await this.context.getDriver();
419
+ const clientConfig = this.context.getConfig();
420
+ const operationPromise = driver.getPrimaryKeys({
421
+ sessionHandle: this.sessionHandle,
422
+ catalogName: request.catalogName,
423
+ schemaName: request.schemaName,
424
+ tableName: request.tableName,
425
+ runAsync: true,
426
+ ...getDirectResultsOptions(request.maxRows, clientConfig),
368
427
  });
428
+ const response = await this.handleResponse(operationPromise);
429
+ return this.createOperation(response);
369
430
  }
370
431
  /**
371
432
  * Request information about foreign keys between two tables
@@ -373,40 +434,47 @@ class DBSQLSession {
373
434
  * @param request
374
435
  * @returns DBSQLOperation
375
436
  */
376
- getCrossReference(request) {
377
- return __awaiter(this, void 0, void 0, function* () {
378
- yield this.failIfClosed();
379
- const driver = yield this.context.getDriver();
380
- const operationPromise = driver.getCrossReference(Object.assign({ sessionHandle: this.sessionHandle, parentCatalogName: request.parentCatalogName, parentSchemaName: request.parentSchemaName, parentTableName: request.parentTableName, foreignCatalogName: request.foreignCatalogName, foreignSchemaName: request.foreignSchemaName, foreignTableName: request.foreignTableName, runAsync: true }, getDirectResultsOptions(request.maxRows)));
381
- const response = yield this.handleResponse(operationPromise);
382
- return this.createOperation(response);
437
+ async getCrossReference(request) {
438
+ await this.failIfClosed();
439
+ const driver = await this.context.getDriver();
440
+ const clientConfig = this.context.getConfig();
441
+ const operationPromise = driver.getCrossReference({
442
+ sessionHandle: this.sessionHandle,
443
+ parentCatalogName: request.parentCatalogName,
444
+ parentSchemaName: request.parentSchemaName,
445
+ parentTableName: request.parentTableName,
446
+ foreignCatalogName: request.foreignCatalogName,
447
+ foreignSchemaName: request.foreignSchemaName,
448
+ foreignTableName: request.foreignTableName,
449
+ runAsync: true,
450
+ ...getDirectResultsOptions(request.maxRows, clientConfig),
383
451
  });
452
+ const response = await this.handleResponse(operationPromise);
453
+ return this.createOperation(response);
384
454
  }
385
455
  /**
386
456
  * Closes the session
387
457
  * @public
388
458
  * @returns Operation status
389
459
  */
390
- close() {
460
+ async close() {
391
461
  var _a;
392
- return __awaiter(this, void 0, void 0, function* () {
393
- if (!this.isOpen) {
394
- return Status_1.default.success();
395
- }
396
- // Close owned operations one by one, removing successfully closed ones from the list
397
- yield this.operations.closeAll();
398
- const driver = yield this.context.getDriver();
399
- const response = yield driver.closeSession({
400
- sessionHandle: this.sessionHandle,
401
- });
402
- // check status for being successful
403
- Status_1.default.assert(response.status);
404
- // notify owner connection
405
- (_a = this.onClose) === null || _a === void 0 ? void 0 : _a.call(this);
406
- this.isOpen = false;
407
- this.context.getLogger().log(IDBSQLLogger_1.LogLevel.debug, `Session closed with id: ${this.getId()}`);
408
- return new Status_1.default(response.status);
462
+ if (!this.isOpen) {
463
+ return Status_1.default.success();
464
+ }
465
+ // Close owned operations one by one, removing successfully closed ones from the list
466
+ await this.operations.closeAll();
467
+ const driver = await this.context.getDriver();
468
+ const response = await driver.closeSession({
469
+ sessionHandle: this.sessionHandle,
409
470
  });
471
+ // check status for being successful
472
+ Status_1.default.assert(response.status);
473
+ // notify owner connection
474
+ (_a = this.onClose) === null || _a === void 0 ? void 0 : _a.call(this);
475
+ this.isOpen = false;
476
+ this.context.getLogger().log(IDBSQLLogger_1.LogLevel.debug, `Session closed with id: ${this.id}`);
477
+ return new Status_1.default(response.status);
410
478
  }
411
479
  createOperation(response) {
412
480
  Status_1.default.assert(response.status);
@@ -419,22 +487,18 @@ class DBSQLSession {
419
487
  this.operations.add(operation);
420
488
  return operation;
421
489
  }
422
- failIfClosed() {
423
- return __awaiter(this, void 0, void 0, function* () {
424
- if (!this.isOpen) {
425
- throw new HiveDriverError_1.default('The session was closed or has expired');
426
- }
427
- });
490
+ async failIfClosed() {
491
+ if (!this.isOpen) {
492
+ throw new HiveDriverError_1.default('The session was closed or has expired');
493
+ }
428
494
  }
429
- handleResponse(requestPromise) {
430
- return __awaiter(this, void 0, void 0, function* () {
431
- // Currently, after being closed sessions remains usable - server will not
432
- // error out when trying to run operations on closed session. So it's
433
- // basically useless to process any errors here
434
- const result = yield requestPromise;
435
- yield this.failIfClosed();
436
- return result;
437
- });
495
+ async handleResponse(requestPromise) {
496
+ // Currently, after being closed sessions remains usable - server will not
497
+ // error out when trying to run operations on closed session. So it's
498
+ // basically useless to process any errors here
499
+ const result = await requestPromise;
500
+ await this.failIfClosed();
501
+ return result;
438
502
  }
439
503
  }
440
504
  exports.default = DBSQLSession;