forked from databricks/databricks-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabricksThriftServiceClient.java
More file actions
616 lines (567 loc) · 26.1 KB
/
DatabricksThriftServiceClient.java
File metadata and controls
616 lines (567 loc) · 26.1 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
package com.databricks.jdbc.dbclient.impl.thrift;
import static com.databricks.jdbc.common.EnvironmentVariables.DEFAULT_STATEMENT_TIMEOUT_SECONDS;
import static com.databricks.jdbc.common.EnvironmentVariables.JDBC_THRIFT_VERSION;
import static com.databricks.jdbc.common.util.DatabricksThriftUtil.*;
import static com.databricks.jdbc.common.util.DatabricksTypeUtil.DECIMAL;
import static com.databricks.jdbc.common.util.DatabricksTypeUtil.getDecimalTypeString;
import static com.databricks.jdbc.dbclient.impl.sqlexec.CommandName.LIST_FUNCTIONS;
import static com.databricks.jdbc.dbclient.impl.sqlexec.ResultConstants.TYPE_INFO_RESULT;
import com.databricks.jdbc.api.impl.*;
import com.databricks.jdbc.api.internal.IDatabricksConnectionContext;
import com.databricks.jdbc.api.internal.IDatabricksSession;
import com.databricks.jdbc.api.internal.IDatabricksStatementInternal;
import com.databricks.jdbc.common.IDatabricksComputeResource;
import com.databricks.jdbc.common.StatementType;
import com.databricks.jdbc.common.util.DatabricksThreadContextHolder;
import com.databricks.jdbc.common.util.DriverUtil;
import com.databricks.jdbc.common.util.ProtocolFeatureUtil;
import com.databricks.jdbc.dbclient.IDatabricksClient;
import com.databricks.jdbc.dbclient.IDatabricksMetadataClient;
import com.databricks.jdbc.dbclient.impl.common.MetadataResultSetBuilder;
import com.databricks.jdbc.dbclient.impl.common.StatementId;
import com.databricks.jdbc.dbclient.impl.sqlexec.CommandBuilder;
import com.databricks.jdbc.exception.DatabricksHttpException;
import com.databricks.jdbc.exception.DatabricksParsingException;
import com.databricks.jdbc.exception.DatabricksSQLException;
import com.databricks.jdbc.log.JdbcLogger;
import com.databricks.jdbc.log.JdbcLoggerFactory;
import com.databricks.jdbc.model.client.thrift.generated.*;
import com.databricks.jdbc.model.core.ExternalLink;
import com.databricks.jdbc.model.telemetry.enums.DatabricksDriverErrorCode;
import com.databricks.sdk.core.DatabricksConfig;
import com.google.common.annotations.VisibleForTesting;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
public class DatabricksThriftServiceClient implements IDatabricksClient, IDatabricksMetadataClient {
private static final JdbcLogger LOGGER =
JdbcLoggerFactory.getLogger(DatabricksThriftServiceClient.class);
private final DatabricksThriftAccessor thriftAccessor;
private final IDatabricksConnectionContext connectionContext;
private TProtocolVersion serverProtocolVersion = JDBC_THRIFT_VERSION;
private final MetadataResultSetBuilder metadataResultSetBuilder;
public DatabricksThriftServiceClient(IDatabricksConnectionContext connectionContext)
throws DatabricksParsingException, DatabricksHttpException {
this.connectionContext = connectionContext;
this.thriftAccessor = new DatabricksThriftAccessor(connectionContext);
this.metadataResultSetBuilder = new MetadataResultSetBuilder(connectionContext);
}
@VisibleForTesting
DatabricksThriftServiceClient(
DatabricksThriftAccessor thriftAccessor, IDatabricksConnectionContext connectionContext) {
this.thriftAccessor = thriftAccessor;
this.connectionContext = connectionContext;
this.metadataResultSetBuilder = new MetadataResultSetBuilder(connectionContext);
}
@VisibleForTesting
void setServerProtocolVersion(TProtocolVersion serverProtocolVersion) {
this.serverProtocolVersion = serverProtocolVersion;
}
@Override
public IDatabricksConnectionContext getConnectionContext() {
return connectionContext;
}
@Override
public void resetAccessToken(String newAccessToken) {
((DatabricksHttpTTransport) thriftAccessor.getThriftClient().getInputProtocol().getTransport())
.resetAccessToken(newAccessToken);
}
@Override
public ImmutableSessionInfo createSession(
IDatabricksComputeResource cluster,
String catalog,
String schema,
Map<String, String> sessionConf)
throws DatabricksSQLException {
LOGGER.debug(
String.format(
"public Session createSession(Compute cluster = {%s}, String catalog = {%s}, String schema = {%s}, Map<String, String> sessionConf = {%s})",
cluster.toString(), catalog, schema, sessionConf));
TOpenSessionReq openSessionReq =
new TOpenSessionReq()
.setConfiguration(sessionConf)
.setCanUseMultipleCatalogs(true)
.setClient_protocol_i64(JDBC_THRIFT_VERSION.getValue());
if (catalog != null || schema != null) {
openSessionReq.setInitialNamespace(getNamespace(catalog, schema));
}
TOpenSessionResp response = (TOpenSessionResp) thriftAccessor.getThriftResponse(openSessionReq);
verifySuccessStatus(response.status, response.toString());
// cache the server protocol version
serverProtocolVersion = response.getServerProtocolVersion();
thriftAccessor.setServerProtocolVersion(
serverProtocolVersion); // save protocol version in thriftAccessor
if (ProtocolFeatureUtil.isNonDatabricksCompute(serverProtocolVersion)) {
throw new DatabricksSQLException(
"Attempting to connect to a non Databricks compute using the Databricks driver.",
DatabricksDriverErrorCode.UNSUPPORTED_OPERATION);
}
String sessionId = byteBufferToString(response.sessionHandle.getSessionId().guid);
DatabricksThreadContextHolder.setSessionId(sessionId);
LOGGER.debug("Session created with ID {}", sessionId);
return ImmutableSessionInfo.builder()
.sessionId(sessionId)
.sessionHandle(response.sessionHandle)
.computeResource(cluster)
.build();
}
@Override
public void deleteSession(ImmutableSessionInfo sessionInfo) throws DatabricksSQLException {
LOGGER.debug(
String.format(
"public void deleteSession(Session session = {%s}))", sessionInfo.toString()));
DatabricksThreadContextHolder.setSessionId(sessionInfo.sessionId());
TCloseSessionReq closeSessionReq =
new TCloseSessionReq().setSessionHandle(sessionInfo.sessionHandle());
TCloseSessionResp response =
(TCloseSessionResp) thriftAccessor.getThriftResponse(closeSessionReq);
verifySuccessStatus(response.status, response.toString());
}
@Override
public DatabricksResultSet executeStatement(
String sql,
IDatabricksComputeResource computeResource,
Map<Integer, ImmutableSqlParameter> parameters,
StatementType statementType,
IDatabricksSession session,
IDatabricksStatementInternal parentStatement)
throws SQLException {
LOGGER.debug(
String.format(
"public DatabricksResultSet executeStatement(String sql = {%s}, Compute cluster = {%s}, Map<Integer, ImmutableSqlParameter> parameters = {%s}, StatementType statementType = {%s}, IDatabricksSession session)",
sql, computeResource, parameters.toString(), statementType));
DatabricksThreadContextHolder.setStatementType(statementType);
TExecuteStatementReq request = getRequest(sql, parameters, session, parentStatement, false);
return thriftAccessor.execute(request, parentStatement, session, statementType);
}
@Override
public DatabricksResultSet executeStatementAsync(
String sql,
IDatabricksComputeResource computeResource,
Map<Integer, ImmutableSqlParameter> parameters,
IDatabricksSession session,
IDatabricksStatementInternal parentStatement)
throws SQLException {
LOGGER.debug(
String.format(
"public DatabricksResultSet executeStatementAsync(String sql = {%s}, Compute cluster = {%s}, Map<Integer, ImmutableSqlParameter> parameters = {%s})",
sql, computeResource.toString(), parameters.toString()));
TExecuteStatementReq request = getRequest(sql, parameters, session, parentStatement, true);
return thriftAccessor.executeAsync(request, parentStatement, session, StatementType.SQL);
}
@VisibleForTesting
TSparkParameter mapToSparkParameterListItem(ImmutableSqlParameter parameter) {
Object value = parameter.value();
String typeString = parameter.type().name();
if (typeString.equals(DECIMAL) && value instanceof BigDecimal) {
typeString = getDecimalTypeString((BigDecimal) value);
}
return new TSparkParameter()
.setOrdinal(parameter.cardinal())
.setType(typeString)
.setValue(value != null ? TSparkParameterValue.stringValue(value.toString()) : null);
}
private TExecuteStatementReq getRequest(
String sql,
Map<Integer, ImmutableSqlParameter> parameters,
IDatabricksSession session,
IDatabricksStatementInternal parentStatement,
boolean runAsync)
throws SQLException {
DatabricksThreadContextHolder.setSessionId(session.getSessionId());
TSparkArrowTypes arrowNativeTypes = new TSparkArrowTypes().setTimestampAsArrow(true);
// Convert the parameters to a list of TSparkParameter objects.
List<TSparkParameter> sparkParameters =
parameters.values().stream()
.map(this::mapToSparkParameterListItem)
.collect(Collectors.toList());
int timeout = DEFAULT_STATEMENT_TIMEOUT_SECONDS;
if (parentStatement != null && parentStatement.getStatement() != null) {
timeout = parentStatement.getStatement().getQueryTimeout();
}
TExecuteStatementReq request =
new TExecuteStatementReq()
.setStatement(sql)
.setQueryTimeout(timeout)
.setSessionHandle(Objects.requireNonNull(session.getSessionInfo()).sessionHandle())
.setCanReadArrowResult(this.connectionContext.shouldEnableArrow())
.setUseArrowNativeTypes(arrowNativeTypes);
// Conditionally set parameters based on server protocol version
if (ProtocolFeatureUtil.supportsParameterizedQueries(serverProtocolVersion)) {
request.setParameters(sparkParameters);
}
if (ProtocolFeatureUtil.supportsCompressedArrowBatches(serverProtocolVersion)) {
request.setCanDecompressLZ4Result(true);
}
if (ProtocolFeatureUtil.supportsCloudFetch(serverProtocolVersion)) {
request.setCanDownloadResult(true);
}
if (ProtocolFeatureUtil.supportsAdvancedArrowTypes(serverProtocolVersion)) {
arrowNativeTypes
.setComplexTypesAsArrow(true)
.setIntervalTypesAsArrow(true)
.setNullTypeAsArrow(true)
.setDecimalAsArrow(true);
request.setUseArrowNativeTypes(arrowNativeTypes);
}
int maxRows = (parentStatement == null) ? 0 : parentStatement.getMaxRows();
if (maxRows > 0) { // set request param only if user has set maxRows.
// Similar
// behavior
// to SEA flow
request.setResultRowLimit(maxRows);
}
if (runAsync || !DriverUtil.isRunningAgainstFake()) {
request.setRunAsync(true);
}
return request;
}
@Override
public void closeStatement(StatementId statementId) throws DatabricksSQLException {
LOGGER.debug(
String.format(
"public void closeStatement(String statementId = {%s}) using Thrift client",
statementId));
DatabricksThreadContextHolder.setStatementId(statementId);
TCloseOperationReq request =
new TCloseOperationReq().setOperationHandle(getOperationHandle(statementId));
TCloseOperationResp resp = thriftAccessor.closeOperation(request);
LOGGER.debug("Statement {} closed with status {}", statementId, resp.getStatus());
}
@Override
public void cancelStatement(StatementId statementId) throws DatabricksSQLException {
LOGGER.debug(
String.format(
"public void cancelStatement(String statementId = {%s}) using Thrift client",
statementId));
DatabricksThreadContextHolder.setStatementId(statementId);
TCancelOperationReq request =
new TCancelOperationReq().setOperationHandle(getOperationHandle(statementId));
TCancelOperationResp resp = thriftAccessor.cancelOperation(request);
LOGGER.debug("Statement {} cancelled with status {}", statementId, resp.getStatus());
}
@Override
public DatabricksResultSet getStatementResult(
StatementId statementId,
IDatabricksSession session,
IDatabricksStatementInternal parentStatement)
throws SQLException {
LOGGER.debug(
String.format(
"public DatabricksResultSet getStatementResult(String statementId = {%s}) using Thrift client",
statementId));
DatabricksThreadContextHolder.setStatementId(statementId);
DatabricksThreadContextHolder.setSessionId(session.getSessionId());
return thriftAccessor.getStatementResult(
getOperationHandle(statementId), parentStatement, session);
}
@Override
public Collection<ExternalLink> getResultChunks(StatementId statementId, long chunkIndex)
throws DatabricksSQLException {
String context =
String.format(
"public Optional<ExternalLink> getResultChunk(String statementId = {%s}, long chunkIndex = {%s}) using Thrift client",
statementId, chunkIndex);
LOGGER.debug(context);
DatabricksThreadContextHolder.setStatementId(statementId);
TFetchResultsResp fetchResultsResp;
List<ExternalLink> externalLinks = new ArrayList<>();
AtomicInteger index = new AtomicInteger(0);
do {
fetchResultsResp = thriftAccessor.getResultSetResp(getOperationHandle(statementId), context);
fetchResultsResp
.getResults()
.getResultLinks()
.forEach(
resultLink ->
externalLinks.add(createExternalLink(resultLink, index.getAndIncrement())));
} while (fetchResultsResp.hasMoreRows);
if (chunkIndex < 0 || externalLinks.size() <= chunkIndex) {
String error = String.format("Out of bounds error for chunkIndex. Context: %s", context);
LOGGER.error(error);
throw new DatabricksSQLException(error, DatabricksDriverErrorCode.INVALID_STATE);
}
return externalLinks;
}
@Override
public DatabricksResultSet listTypeInfo(IDatabricksSession session) {
LOGGER.debug("public ResultSet getTypeInfo()");
return TYPE_INFO_RESULT;
}
@Override
public DatabricksResultSet listCatalogs(IDatabricksSession session) throws SQLException {
String context =
String.format("Fetching catalogs using Thrift client. Session {%s}", session.toString());
LOGGER.debug(context);
DatabricksThreadContextHolder.setSessionId(session.getSessionId());
TGetCatalogsReq request =
new TGetCatalogsReq()
.setSessionHandle(Objects.requireNonNull(session.getSessionInfo()).sessionHandle());
if (ProtocolFeatureUtil.supportsAsyncMetadataExecution(serverProtocolVersion)) {
request.setRunAsync(true); // support async metadata execution if supported
}
TFetchResultsResp response = (TFetchResultsResp) thriftAccessor.getThriftResponse(request);
return metadataResultSetBuilder.getCatalogsResult(
extractRowsFromColumnar(response.getResults()));
}
@Override
public DatabricksResultSet listSchemas(
IDatabricksSession session, String catalog, String schemaNamePattern) throws SQLException {
String context =
String.format(
"Fetching schemas using Thrift client. Session {%s}, catalog {%s}, schemaNamePattern {%s}",
session.toString(), catalog, schemaNamePattern);
LOGGER.debug(context);
DatabricksThreadContextHolder.setSessionId(session.getSessionId());
TGetSchemasReq request =
new TGetSchemasReq()
.setSessionHandle(Objects.requireNonNull(session.getSessionInfo()).sessionHandle())
.setCatalogName(catalog);
if (schemaNamePattern != null) {
request.setSchemaName(schemaNamePattern);
}
if (ProtocolFeatureUtil.supportsAsyncMetadataExecution(serverProtocolVersion)) {
request.setRunAsync(true);
}
TFetchResultsResp response = (TFetchResultsResp) thriftAccessor.getThriftResponse(request);
return metadataResultSetBuilder.getSchemasResult(
extractRowsFromColumnar(response.getResults()));
}
@Override
public DatabricksResultSet listTables(
IDatabricksSession session,
String catalog,
String schemaNamePattern,
String tableNamePattern,
String[] tableTypes)
throws SQLException {
String context =
String.format(
"Fetching tables using Thrift client. Session {%s}, catalog {%s}, schemaNamePattern {%s}, tableNamePattern {%s}",
session.toString(), catalog, schemaNamePattern, tableNamePattern);
LOGGER.debug(context);
DatabricksThreadContextHolder.setSessionId(session.getSessionId());
TGetTablesReq request =
new TGetTablesReq()
.setSessionHandle(Objects.requireNonNull(session.getSessionInfo()).sessionHandle())
.setCatalogName(catalog)
.setSchemaName(schemaNamePattern)
.setTableName(tableNamePattern);
if (tableTypes != null) {
request.setTableTypes(Arrays.asList(tableTypes));
}
if (ProtocolFeatureUtil.supportsAsyncMetadataExecution(serverProtocolVersion)) {
request.setRunAsync(true);
}
TFetchResultsResp response = (TFetchResultsResp) thriftAccessor.getThriftResponse(request);
return metadataResultSetBuilder.getTablesResult(
catalog, tableTypes, extractRowsFromColumnar(response.getResults()));
}
@Override
public DatabricksResultSet listTableTypes(IDatabricksSession session) {
LOGGER.debug(
String.format(
"Fetching table types using Thrift client. Session {%s}", session.toString()));
DatabricksThreadContextHolder.setSessionId(session.getSessionId());
return metadataResultSetBuilder.getTableTypesResult();
}
@Override
public DatabricksResultSet listColumns(
IDatabricksSession session,
String catalog,
String schemaNamePattern,
String tableNamePattern,
String columnNamePattern)
throws DatabricksSQLException {
String context =
String.format(
"Fetching columns using Thrift client. Session {%s}, catalog {%s}, schemaNamePattern {%s}, tableNamePattern {%s}, columnNamePattern {%s}",
session.toString(), catalog, schemaNamePattern, tableNamePattern, columnNamePattern);
LOGGER.debug(context);
DatabricksThreadContextHolder.setSessionId(session.getSessionId());
TGetColumnsReq request =
new TGetColumnsReq()
.setSessionHandle(Objects.requireNonNull(session.getSessionInfo()).sessionHandle())
.setCatalogName(catalog)
.setSchemaName(schemaNamePattern)
.setTableName(tableNamePattern)
.setColumnName(columnNamePattern);
if (ProtocolFeatureUtil.supportsAsyncMetadataExecution(serverProtocolVersion)) {
request.setRunAsync(true);
}
TFetchResultsResp response = (TFetchResultsResp) thriftAccessor.getThriftResponse(request);
return metadataResultSetBuilder.getColumnsResult(
extractRowsFromColumnar(response.getResults()));
}
@Override
public DatabricksResultSet listFunctions(
IDatabricksSession session,
String catalog,
String schemaNamePattern,
String functionNamePattern)
throws SQLException {
String context =
String.format(
"Fetching functions using Thrift client. Session {%s}, catalog {%s}, schemaNamePattern {%s}, functionNamePattern {%s}.",
session.toString(), catalog, schemaNamePattern, functionNamePattern);
DatabricksThreadContextHolder.setSessionId(session.getSessionId());
LOGGER.debug(context);
if (connectionContext.enableShowCommandsForGetFunctions()) {
String showFunctionsSqlCommand =
new CommandBuilder(catalog, session)
.setSchemaPattern(schemaNamePattern)
.setFunctionPattern(functionNamePattern)
.getSQLString(LIST_FUNCTIONS);
LOGGER.debug(
"Fetching functions using SQL Command {{}}. Session {{}}",
showFunctionsSqlCommand,
session.toString());
try (DatabricksResultSet rs =
executeStatement(
showFunctionsSqlCommand,
session.getComputeResource(),
Collections.emptyMap(),
StatementType.METADATA,
session,
null)) {
return metadataResultSetBuilder.getFunctionsResult(rs, catalog);
}
}
TGetFunctionsReq request =
new TGetFunctionsReq()
.setSessionHandle(Objects.requireNonNull(session.getSessionInfo()).sessionHandle())
.setCatalogName(catalog)
.setSchemaName(schemaNamePattern)
.setFunctionName(functionNamePattern);
if (ProtocolFeatureUtil.supportsAsyncMetadataExecution(serverProtocolVersion)) {
request.setRunAsync(true);
}
TFetchResultsResp response = (TFetchResultsResp) thriftAccessor.getThriftResponse(request);
return metadataResultSetBuilder.getFunctionsResult(
catalog, extractRowsFromColumnar(response.getResults()));
}
@Override
public DatabricksResultSet listPrimaryKeys(
IDatabricksSession session, String catalog, String schema, String table) throws SQLException {
String context =
String.format(
"Fetching primary keys using Thrift client. session {%s}, catalog {%s}, schema {%s}, table {%s}",
session.toString(), catalog, schema, table);
LOGGER.debug(context);
DatabricksThreadContextHolder.setSessionId(session.getSessionId());
TGetPrimaryKeysReq request =
new TGetPrimaryKeysReq()
.setSessionHandle(Objects.requireNonNull(session.getSessionInfo()).sessionHandle())
.setCatalogName(catalog)
.setSchemaName(schema)
.setTableName(table);
if (ProtocolFeatureUtil.supportsAsyncMetadataExecution(serverProtocolVersion)) {
request.setRunAsync(true);
}
TFetchResultsResp response = (TFetchResultsResp) thriftAccessor.getThriftResponse(request);
return metadataResultSetBuilder.getPrimaryKeysResult(
extractRowsFromColumnar(response.getResults()));
}
@Override
public DatabricksResultSet listImportedKeys(
IDatabricksSession session, String catalog, String schema, String table) throws SQLException {
String context =
String.format(
"Fetching imported keys using Thrift client for session {%s}, catalog {%s}, schema {%s}, table {%s}",
session.toString(), catalog, schema, table);
LOGGER.debug(context);
DatabricksThreadContextHolder.setSessionId(session.getSessionId());
// GetImportedKeys is implemented using GetCrossReferences
// When only foreign table name is provided, we get imported keys
TGetCrossReferenceReq request =
new TGetCrossReferenceReq()
.setSessionHandle(Objects.requireNonNull(session.getSessionInfo()).sessionHandle())
.setForeignCatalogName(catalog)
.setForeignSchemaName(schema)
.setForeignTableName(table);
if (ProtocolFeatureUtil.supportsAsyncMetadataExecution(serverProtocolVersion)) {
request.setRunAsync(true);
}
TFetchResultsResp response = (TFetchResultsResp) thriftAccessor.getThriftResponse(request);
return metadataResultSetBuilder.getImportedKeys(extractRowsFromColumnar(response.getResults()));
}
@Override
public DatabricksResultSet listExportedKeys(
IDatabricksSession session, String catalog, String schema, String table) throws SQLException {
String context =
String.format(
"Fetching exported keys using Thrift client for session {%s}, catalog {%s}, schema {%s}, table {%s}",
session.toString(), catalog, schema, table);
LOGGER.debug(context);
// GetImportedKeys is implemented using GetCrossReferences
// When only parent table name is provided, we get exported keys
TGetCrossReferenceReq request =
new TGetCrossReferenceReq()
.setSessionHandle(Objects.requireNonNull(session.getSessionInfo()).sessionHandle())
.setParentCatalogName(catalog)
.setParentSchemaName(schema)
.setParentTableName(table);
if (ProtocolFeatureUtil.supportsAsyncMetadataExecution(serverProtocolVersion)) {
request.setRunAsync(true);
}
TFetchResultsResp response = (TFetchResultsResp) thriftAccessor.getThriftResponse(request);
return metadataResultSetBuilder.getExportedKeys(extractRowsFromColumnar(response.getResults()));
}
@Override
public DatabricksResultSet listCrossReferences(
IDatabricksSession session,
String parentCatalog,
String parentSchema,
String parentTable,
String foreignCatalog,
String foreignSchema,
String foreignTable)
throws SQLException {
String context =
String.format(
"Fetching cross references using Thrift client for session {%s}, catalog {%s}, schema {%s}, table {%s}, foreign catalog {%s}, foreign schema {%s}, foreign table {%s}",
session.toString(),
parentCatalog,
parentSchema,
parentTable,
foreignCatalog,
foreignSchema,
foreignTable);
LOGGER.debug(context);
TGetCrossReferenceReq request =
new TGetCrossReferenceReq()
.setSessionHandle(Objects.requireNonNull(session.getSessionInfo()).sessionHandle())
.setParentCatalogName(parentCatalog)
.setParentSchemaName(parentSchema)
.setParentTableName(parentTable)
.setForeignCatalogName(foreignCatalog)
.setForeignSchemaName(foreignSchema)
.setForeignTableName(foreignTable);
if (ProtocolFeatureUtil.supportsAsyncMetadataExecution(serverProtocolVersion)) {
request.setRunAsync(true);
}
TFetchResultsResp response = (TFetchResultsResp) thriftAccessor.getThriftResponse(request);
return metadataResultSetBuilder.getCrossRefsResult(
extractRowsFromColumnar(response.getResults()));
}
public TFetchResultsResp getMoreResults(IDatabricksStatementInternal parentStatement)
throws DatabricksSQLException {
return thriftAccessor.getMoreResults(parentStatement);
}
@Override
public DatabricksConfig getDatabricksConfig() {
return thriftAccessor.getDatabricksConfig();
}
private TNamespace getNamespace(String catalog, String schema) {
final TNamespace namespace = new TNamespace();
if (catalog != null) {
namespace.setCatalogName(catalog);
}
if (schema != null) {
namespace.setSchemaName(schema);
}
return namespace;
}
}