forked from databricks/databricks-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabricksResultSet.java
More file actions
2084 lines (1835 loc) · 75.3 KB
/
DatabricksResultSet.java
File metadata and controls
2084 lines (1835 loc) · 75.3 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
package com.databricks.jdbc.api.impl;
import static com.databricks.jdbc.common.DatabricksJdbcConstants.EMPTY_STRING;
import static com.databricks.jdbc.common.util.DatabricksThriftUtil.getArrowMetadata;
import static com.databricks.jdbc.common.util.DatabricksTypeUtil.*;
import com.databricks.jdbc.api.IDatabricksResultSet;
import com.databricks.jdbc.api.IExecutionStatus;
import com.databricks.jdbc.api.impl.arrow.ArrowStreamResult;
import com.databricks.jdbc.api.impl.arrow.ChunkProvider;
import com.databricks.jdbc.api.impl.arrow.LazyThriftInlineArrowResult;
import com.databricks.jdbc.api.impl.arrow.StreamingInlineArrowResult;
import com.databricks.jdbc.api.impl.converters.ConverterHelper;
import com.databricks.jdbc.api.impl.converters.ObjectConverter;
import com.databricks.jdbc.api.impl.thrift.StreamingColumnarResult;
import com.databricks.jdbc.api.impl.volume.VolumeOperationResult;
import com.databricks.jdbc.api.internal.IDatabricksConnectionContext;
import com.databricks.jdbc.api.internal.IDatabricksResultSetInternal;
import com.databricks.jdbc.api.internal.IDatabricksSession;
import com.databricks.jdbc.api.internal.IDatabricksStatementInternal;
import com.databricks.jdbc.common.Nullable;
import com.databricks.jdbc.common.StatementType;
import com.databricks.jdbc.common.util.WarningUtil;
import com.databricks.jdbc.dbclient.impl.common.StatementId;
import com.databricks.jdbc.exception.DatabricksParsingException;
import com.databricks.jdbc.exception.DatabricksSQLException;
import com.databricks.jdbc.exception.DatabricksSQLFeatureNotSupportedException;
import com.databricks.jdbc.exception.DatabricksValidationException;
import com.databricks.jdbc.log.JdbcLogger;
import com.databricks.jdbc.log.JdbcLoggerFactory;
import com.databricks.jdbc.model.client.thrift.generated.TFetchResultsResp;
import com.databricks.jdbc.model.core.*;
import com.databricks.jdbc.model.telemetry.enums.DatabricksDriverErrorCode;
import com.databricks.jdbc.telemetry.latency.TelemetryCollector;
import com.databricks.jdbc.telemetry.latency.TelemetryCollectorManager;
import com.databricks.sdk.support.ToStringer;
import com.google.common.annotations.VisibleForTesting;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.sql.*;
import java.time.*;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import org.apache.http.entity.InputStreamEntity;
public class DatabricksResultSet implements IDatabricksResultSet, IDatabricksResultSetInternal {
enum ResultSetType {
SEA_ARROW_ENABLED,
SEA_INLINE,
THRIFT_ARROW_ENABLED,
THRIFT_INLINE,
UNASSIGNED
}
private static final JdbcLogger LOGGER = JdbcLoggerFactory.getLogger(DatabricksResultSet.class);
protected static final String AFFECTED_ROWS_COUNT = "num_affected_rows";
private final ExecutionStatus executionStatus;
private final StatementId statementId;
private final IExecutionResult executionResult;
private final DatabricksResultSetMetaData resultSetMetaData;
private final StatementType statementType;
private final IDatabricksStatementInternal parentStatement;
private Long updateCount;
private boolean isClosed;
private SQLWarning warnings = null;
private boolean wasNull;
private boolean silenceNonTerminalExceptions = false;
private ResultSetType resultSetType = ResultSetType.UNASSIGNED;
private boolean complexDatatypeSupport = false;
// Cached telemetry collector resolved once at construction time to avoid
// per-row overhead in next(). The connection-to-collector mapping is stable
// for the lifetime of a result set.
private final TelemetryCollector cachedTelemetryCollector;
// Constructor for SEA result set
public DatabricksResultSet(
StatementStatus statementStatus,
StatementId statementId,
ResultData resultData,
ResultManifest resultManifest,
StatementType statementType,
IDatabricksSession session,
IDatabricksStatementInternal parentStatement)
throws SQLException {
this.executionStatus = new ExecutionStatus(statementStatus);
this.statementId = statementId;
if (resultData != null) {
this.executionResult =
ExecutionResultFactory.getResultSet(
resultData, resultManifest, statementId, session, parentStatement);
this.resultSetMetaData =
new DatabricksResultSetMetaData(
statementId,
resultManifest,
resultData.getExternalLinks() != null,
session.getConnectionContext());
switch (resultManifest.getFormat()) {
case ARROW_STREAM:
this.resultSetType = ResultSetType.SEA_ARROW_ENABLED;
break;
case JSON_ARRAY:
this.resultSetType = ResultSetType.SEA_INLINE;
break;
}
} else {
executionResult = null;
resultSetMetaData = null;
}
this.complexDatatypeSupport = session.getConnectionContext().isComplexDatatypeSupportEnabled();
this.statementType = statementType;
this.updateCount = null;
this.parentStatement = parentStatement;
this.cachedTelemetryCollector = resolveTelemetryCollector(parentStatement);
this.isClosed = false;
this.wasNull = false;
}
@VisibleForTesting
public DatabricksResultSet(
StatementStatus statementStatus,
StatementId statementId,
StatementType statementType,
IDatabricksStatementInternal parentStatement,
IExecutionResult executionResult,
DatabricksResultSetMetaData resultSetMetaData,
boolean complexDatatypeSupport) {
this.executionStatus = new ExecutionStatus(statementStatus);
this.statementId = statementId;
this.executionResult = executionResult;
this.resultSetMetaData = resultSetMetaData;
this.statementType = statementType;
this.updateCount = null;
this.parentStatement = parentStatement;
this.cachedTelemetryCollector = resolveTelemetryCollector(parentStatement);
this.isClosed = false;
this.wasNull = false;
this.complexDatatypeSupport = complexDatatypeSupport;
}
// Constructor for thrift result set
public DatabricksResultSet(
StatementStatus statementStatus,
StatementId statementId,
TFetchResultsResp resultsResp,
StatementType statementType,
IDatabricksStatementInternal parentStatement,
IDatabricksSession session)
throws SQLException {
this.executionStatus = new ExecutionStatus(statementStatus);
this.statementId = statementId;
if (resultsResp != null) {
this.executionResult =
ExecutionResultFactory.getResultSet(resultsResp, session, parentStatement);
long rowSize = executionResult.getRowCount();
List<String> arrowMetadata = getArrowMetadata(resultsResp.getResultSetMetadata());
this.resultSetMetaData =
new DatabricksResultSetMetaData(
statementId,
resultsResp.getResultSetMetadata(),
rowSize,
executionResult.getChunkCount(),
arrowMetadata,
session.getConnectionContext());
switch (resultsResp.getResultSetMetadata().getResultFormat()) {
case COLUMN_BASED_SET:
this.resultSetType = ResultSetType.THRIFT_INLINE;
break;
case URL_BASED_SET:
case ARROW_BASED_SET:
this.resultSetType = ResultSetType.THRIFT_ARROW_ENABLED;
break;
}
} else {
this.executionResult = null;
this.resultSetMetaData = null;
}
this.complexDatatypeSupport = session.getConnectionContext().isComplexDatatypeSupportEnabled();
this.statementType = statementType;
this.updateCount = null;
this.parentStatement = parentStatement;
this.cachedTelemetryCollector = resolveTelemetryCollector(parentStatement);
this.isClosed = false;
this.wasNull = false;
}
/* Constructing results for getUDTs, getTypeInfo, getProcedures metadata calls */
public DatabricksResultSet(
StatementStatus statementStatus,
StatementId statementId,
List<String> columnNames,
List<String> columnTypeText,
int[] columnTypes,
int[] columnTypePrecisions,
int[] isNullables,
Object[][] rows,
StatementType statementType) {
this.executionStatus = new ExecutionStatus(statementStatus);
this.statementId = statementId;
this.executionResult = ExecutionResultFactory.getResultSet(rows);
this.resultSetMetaData =
new DatabricksResultSetMetaData(
statementId,
columnNames,
columnTypeText,
columnTypes,
columnTypePrecisions,
isNullables,
rows.length);
this.statementType = statementType;
this.updateCount = null;
this.parentStatement = null;
this.cachedTelemetryCollector = null;
this.isClosed = false;
this.wasNull = false;
}
// Constructing metadata result set in thrift flow
public DatabricksResultSet(
StatementStatus statementStatus,
StatementId statementId,
List<String> columnNames,
List<String> columnTypeText,
List<Integer> columnTypes,
List<Integer> columnTypePrecisions,
List<Nullable> columnNullables,
List<List<Object>> rows,
StatementType statementType) {
this.executionStatus = new ExecutionStatus(statementStatus);
this.statementId = statementId;
this.executionResult = ExecutionResultFactory.getResultSet(rows);
this.resultSetMetaData =
new DatabricksResultSetMetaData(
statementId,
columnNames,
columnTypeText,
columnTypes,
columnTypePrecisions,
columnNullables,
rows.size());
this.statementType = statementType;
this.updateCount = null;
this.parentStatement = null;
this.cachedTelemetryCollector = null;
this.isClosed = false;
this.wasNull = false;
}
// Constructing metadata result set in SEA flow
public DatabricksResultSet(
StatementStatus statementStatus,
StatementId statementId,
List<ColumnMetadata> columnMetadataList,
List<List<Object>> rows,
StatementType statementType) {
this.executionStatus = new ExecutionStatus(statementStatus);
this.statementId = statementId;
this.executionResult = ExecutionResultFactory.getResultSet(rows);
this.resultSetMetaData =
new DatabricksResultSetMetaData(statementId, columnMetadataList, rows.size());
this.statementType = statementType;
this.updateCount = null;
this.parentStatement = null;
this.cachedTelemetryCollector = null;
this.isClosed = false;
this.wasNull = false;
}
@Override
public boolean next() throws SQLException {
checkIfClosed();
boolean hasNext = this.executionResult.next();
if (cachedTelemetryCollector != null) {
cachedTelemetryCollector.recordResultSetIteration(
statementId.toSQLExecStatementId(), resultSetMetaData.getChunkCount(), hasNext);
}
return hasNext;
}
@Override
public void close() throws DatabricksSQLException {
// Proactively close server operation when ResultSet is closed explicitly.
closeServerOperation();
isClosed = true;
this.executionResult.close();
if (parentStatement != null) {
parentStatement.handleResultSetClose(this);
}
}
/** Proactively closes the server-side operation via the parent statement. */
private void closeServerOperation() {
if (parentStatement != null) {
parentStatement.closeServerOperation();
}
}
private static TelemetryCollector resolveTelemetryCollector(
IDatabricksStatementInternal parentStatement) {
try {
if (parentStatement != null) {
IDatabricksConnectionContext connectionContext =
((DatabricksConnection) parentStatement.getStatement().getConnection())
.getConnectionContext();
if (connectionContext != null) {
return TelemetryCollectorManager.getInstance().getOrCreateCollector(connectionContext);
}
}
} catch (Exception e) {
LOGGER.trace("Error resolving telemetry collector: {}", e.getMessage());
}
return null;
}
@Override
public boolean wasNull() throws SQLException {
checkIfClosed();
return this.wasNull;
}
@Override
public String getString(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toString, () -> null);
}
@Override
public boolean getBoolean(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toBoolean, () -> false);
}
@Override
public byte getByte(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toByte, () -> (byte) 0);
}
@Override
public short getShort(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toShort, () -> (short) 0);
}
@Override
public int getInt(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toInt, () -> 0);
}
@Override
public long getLong(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toLong, () -> 0L);
}
@Override
public float getFloat(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toFloat, () -> 0.0f);
}
@Override
public double getDouble(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toDouble, () -> 0.0);
}
@Override
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
return getConvertedObject(
columnIndex,
(converter, object) -> {
BigDecimal bd = converter.toBigDecimal(object);
return applyScaleToBigDecimal(bd, columnIndex, scale);
},
() -> null);
}
@Override
public byte[] getBytes(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toByteArray, () -> null);
}
@Override
public Date getDate(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toDate, () -> null);
}
@Override
public Time getTime(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toTime, () -> null);
}
@Override
public Timestamp getTimestamp(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toTimestamp, () -> null);
}
@Override
public InputStream getAsciiStream(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toAsciiStream, () -> null);
}
@Override
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toUnicodeStream, () -> null);
}
@Override
public InputStream getBinaryStream(int columnIndex) throws SQLException {
return getConvertedObject(columnIndex, ObjectConverter::toBinaryStream, () -> null);
}
@Override
public String getString(String columnLabel) throws SQLException {
return getString(getColumnNameIndex(columnLabel));
}
@Override
public boolean getBoolean(String columnLabel) throws SQLException {
return getBoolean(getColumnNameIndex(columnLabel));
}
@Override
public byte getByte(String columnLabel) throws SQLException {
return getByte(getColumnNameIndex(columnLabel));
}
@Override
public short getShort(String columnLabel) throws SQLException {
return getShort(getColumnNameIndex(columnLabel));
}
@Override
public int getInt(String columnLabel) throws SQLException {
return getInt(getColumnNameIndex(columnLabel));
}
@Override
public long getLong(String columnLabel) throws SQLException {
return getLong(getColumnNameIndex(columnLabel));
}
@Override
public float getFloat(String columnLabel) throws SQLException {
return getFloat(getColumnNameIndex(columnLabel));
}
@Override
public double getDouble(String columnLabel) throws SQLException {
return getDouble(getColumnNameIndex(columnLabel));
}
@Override
public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException {
return getBigDecimal(getColumnNameIndex(columnLabel));
}
@Override
public byte[] getBytes(String columnLabel) throws SQLException {
return getBytes(getColumnNameIndex(columnLabel));
}
@Override
public Date getDate(String columnLabel) throws SQLException {
return getDate(getColumnNameIndex(columnLabel));
}
@Override
public Time getTime(String columnLabel) throws SQLException {
return getTime(getColumnNameIndex(columnLabel));
}
@Override
public Timestamp getTimestamp(String columnLabel) throws SQLException {
return getTimestamp(getColumnNameIndex(columnLabel));
}
@Override
public InputStream getAsciiStream(String columnLabel) throws SQLException {
return getAsciiStream(getColumnNameIndex(columnLabel));
}
@Override
public InputStream getUnicodeStream(String columnLabel) throws SQLException {
return getUnicodeStream(getColumnNameIndex(columnLabel));
}
@Override
public InputStream getBinaryStream(String columnLabel) throws SQLException {
return getBinaryStream(getColumnNameIndex(columnLabel));
}
@Override
public SQLWarning getWarnings() throws SQLException {
checkIfClosed();
return warnings;
}
@Override
public void clearWarnings() throws SQLException {
checkIfClosed();
warnings = null;
}
@Override
public String getCursorName() throws SQLException {
checkIfClosed();
return EMPTY_STRING;
}
@Override
public ResultSetMetaData getMetaData() throws SQLException {
return resultSetMetaData;
}
/**
* Checks if the given type name represents a geospatial type (GEOMETRY or GEOGRAPHY).
*
* @param typeName The type name to check
* @return true if the type name starts with GEOMETRY or GEOGRAPHY, false otherwise
*/
private static boolean isGeospatialType(String typeName) {
return typeName.startsWith(GEOMETRY) || typeName.startsWith(GEOGRAPHY);
}
@Override
public Object getObject(int columnIndex) throws SQLException {
checkIfClosed();
Object obj = getObjectInternal(columnIndex);
if (obj == null) {
return null;
}
int columnType = resultSetMetaData.getColumnType(columnIndex);
String columnTypeName = resultSetMetaData.getColumnTypeName(columnIndex);
// Geospatial types: handle independently of complex datatype flag
if (isGeospatialType(columnTypeName)) {
return handleGeospatialType(obj, columnTypeName);
}
// separate handling for complex data types
if (isComplexType(columnTypeName)) {
return handleComplexDataTypes(obj, columnTypeName);
}
// VARIANT types should only accept String objects
if (VARIANT.equals(columnTypeName)) {
if (!(obj instanceof String)) {
throw new DatabricksValidationException(
"VARIANT type only supports String objects, got: " + obj.getClass().getSimpleName());
}
}
// TODO: Add separate handling for INTERVAL JSON_ARRAY result format.
return ConverterHelper.convertSqlTypeToJavaType(columnType, obj);
}
private Object handleGeospatialType(Object obj, String columnName) throws DatabricksSQLException {
if (resultSetType == ResultSetType.SEA_INLINE) {
obj = convertGeospatialForSEAInline(obj, columnName);
}
return obj;
}
private Object convertGeospatialForSEAInline(Object obj, String columnName)
throws DatabricksSQLException {
if (columnName.startsWith(GEOMETRY)) {
return ConverterHelper.getConverterForColumnType(Types.OTHER, GEOMETRY)
.toDatabricksGeometry(obj);
} else if (columnName.startsWith(GEOGRAPHY)) {
return ConverterHelper.getConverterForColumnType(Types.OTHER, GEOGRAPHY)
.toDatabricksGeography(obj);
}
return obj;
}
private Object handleComplexDataTypes(Object obj, String columnName)
throws DatabricksSQLException {
if (resultSetType == ResultSetType.SEA_INLINE) {
obj = convertToComplexDataTypesForSEAInline(obj, columnName);
}
return complexDatatypeSupport ? obj : obj.toString();
}
private Object convertToComplexDataTypesForSEAInline(Object obj, String columnName)
throws DatabricksSQLException {
ComplexDataTypeParser parser = new ComplexDataTypeParser();
if (columnName.startsWith(ARRAY)) {
return parser.parseJsonStringToDbArray(obj.toString(), columnName);
} else if (columnName.startsWith(MAP)) {
return parser.parseJsonStringToDbMap(obj.toString(), columnName);
} else if (columnName.startsWith(STRUCT)) {
return parser.parseJsonStringToDbStruct(obj.toString(), columnName);
}
throw new DatabricksParsingException(
"Unexpected metadata format. Type is not a COMPLEX: " + columnName,
DatabricksDriverErrorCode.JSON_PARSING_ERROR,
silenceNonTerminalExceptions);
}
@Override
public Object getObject(String columnLabel) throws SQLException {
checkIfClosed();
return getObject(getColumnNameIndex(columnLabel));
}
@Override
public int findColumn(String columnLabel) throws SQLException {
checkIfClosed();
int columnIndex = getColumnNameIndex(columnLabel);
if (columnIndex == -1) {
LOGGER.error("Column not found: {}", columnLabel);
throw new DatabricksSQLException(
"Column not found: " + columnLabel,
DatabricksDriverErrorCode.RESULT_SET_ERROR,
silenceNonTerminalExceptions);
}
return columnIndex;
}
@Override
public Reader getCharacterStream(int columnIndex) throws SQLException {
checkIfClosed();
Object obj = getObjectInternal(columnIndex);
if (obj == null) {
return null;
}
int columnType = resultSetMetaData.getColumnType(columnIndex);
ObjectConverter converter = ConverterHelper.getConverterForSqlType(columnType);
return converter.toCharacterStream(obj);
}
@Override
public Reader getCharacterStream(String columnLabel) throws SQLException {
return getCharacterStream(getColumnNameIndex(columnLabel));
}
@Override
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
return getBigDecimal(columnIndex, resultSetMetaData.getScale(columnIndex));
}
@Override
public BigDecimal getBigDecimal(String columnLabel) throws SQLException {
return getBigDecimal(getColumnNameIndex(columnLabel));
}
@Override
public boolean isBeforeFirst() throws SQLException {
checkIfClosed();
return executionResult.getCurrentRow() == -1;
}
/**
* {@inheritDoc}
*
* <p><b>Limitation:</b> For lazy/streaming result sets ({@link LazyThriftResult}, {@link
* StreamingColumnarResult}, {@link LazyThriftInlineArrowResult}, {@link
* StreamingInlineArrowResult}), this method cannot reliably determine the cursor position. The
* total row count remains unknown until all rows are fetched, preventing accurate detection of
* whether the cursor is after the last row. This is specific to Databricks JDBC dialect.
*/
@Override
public boolean isAfterLast() throws SQLException {
checkIfClosed();
return executionResult.getCurrentRow() >= resultSetMetaData.getTotalRows();
}
@Override
public boolean isFirst() throws SQLException {
checkIfClosed();
return executionResult.getCurrentRow() == 0;
}
/**
* {@inheritDoc}
*
* <p>This method uses different strategies based on the result set type:
*
* <ul>
* <li>For lazy/streaming result types ({@link LazyThriftResult}, {@link
* StreamingColumnarResult}, {@link LazyThriftInlineArrowResult}, {@link
* StreamingInlineArrowResult}): Checks if there are no more rows available (using {@code
* hasNext()}), since the total row count is unknown until all rows are fetched.
* <li>For other result types: Compares the current row position against the known total row
* count.
* </ul>
*
* @return {@code true} if the cursor is on the last row, {@code false} otherwise
* @throws SQLException if the result set is closed or an error occurs
*/
@Override
public boolean isLast() throws SQLException {
checkIfClosed();
if (executionResult instanceof LazyThriftResult
|| executionResult instanceof StreamingColumnarResult
|| executionResult instanceof LazyThriftInlineArrowResult
|| executionResult instanceof StreamingInlineArrowResult) {
return executionResult.getCurrentRow() >= 0 && !executionResult.hasNext();
}
return executionResult.getCurrentRow() == resultSetMetaData.getTotalRows() - 1;
}
@Override
public void beforeFirst() throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC does not support random access (beforeFirst)");
}
@Override
public void afterLast() throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC does not support random access (afterLast)");
}
@Override
public boolean first() throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC does not support random access (first)");
}
@Override
public boolean last() throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC does not support random access (last)");
}
@Override
public int getRow() throws SQLException {
checkIfClosed();
return (int) executionResult.getCurrentRow() + 1;
}
@Override
public boolean absolute(int row) throws SQLException {
checkIfClosed();
if (row < 1 || row < executionResult.getCurrentRow()) {
throw new DatabricksSQLFeatureNotSupportedException(
"Invalid operation for forward only ResultSets");
}
while (executionResult.getCurrentRow() < row - 1) {
if (!next()) {
return false;
}
}
return true;
}
@Override
public boolean relative(int rows) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC does not support random access (relative)");
}
@Override
public boolean previous() throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC does not support random access (previous)");
}
@Override
public void setFetchDirection(int direction) throws SQLException {
checkIfClosed();
// Only allow forward direction
if (direction != ResultSet.FETCH_FORWARD) {
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC only supports FETCH_FORWARD direction");
}
}
@Override
public int getFetchDirection() throws SQLException {
checkIfClosed();
return ResultSet.FETCH_FORWARD;
}
@Override
public void setFetchSize(int rows) throws SQLException {
/* As we fetch chunks of data together,
setting fetchSize is an overkill.
Hence, we don't support it.*/
LOGGER.debug("public void setFetchSize(int rows = {})", rows);
checkIfClosed();
String warningString = "As FetchSize is not supported in the Databricks JDBC, ignoring it";
LOGGER.debug(warningString);
warnings = WarningUtil.addWarning(warnings, warningString);
}
@Override
public int getFetchSize() throws SQLException {
LOGGER.debug("public int getFetchSize()");
checkIfClosed();
String warningString =
"As FetchSize is not supported in the Databricks JDBC, we don't set it in the first place";
LOGGER.debug(warningString);
warnings = WarningUtil.addWarning(warnings, warningString);
return 0;
}
@Override
public int getType() throws SQLException {
checkIfClosed();
return ResultSet.TYPE_FORWARD_ONLY;
}
@Override
public int getConcurrency() throws SQLException {
checkIfClosed();
return ResultSet.CONCUR_READ_ONLY;
}
@Override
public boolean rowUpdated() throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support the function : rowUpdated");
}
@Override
public boolean rowInserted() throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support the function : rowInserted");
}
@Override
public boolean rowDeleted() throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support the function : rowDeleted");
}
@Override
public void updateNull(int columnIndex) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateNull");
}
@Override
public void updateBoolean(int columnIndex, boolean x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateBoolean");
}
@Override
public void updateByte(int columnIndex, byte x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateByte");
}
@Override
public void updateShort(int columnIndex, short x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateShort");
}
@Override
public void updateInt(int columnIndex, int x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateInt");
}
@Override
public void updateLong(int columnIndex, long x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateLong");
}
@Override
public void updateFloat(int columnIndex, float x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateFloat");
}
@Override
public void updateDouble(int columnIndex, double x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateDouble");
}
@Override
public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException {
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateBigDecimal");
}
@Override
public void updateString(int columnIndex, String x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateString");
}
@Override
public void updateBytes(int columnIndex, byte[] x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateBytes");
}
@Override
public void updateDate(int columnIndex, Date x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateDate");
}
@Override
public void updateTime(int columnIndex, Time x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateTime");
}
@Override
public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateTimestamp");
}
@Override
public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateAsciiStream");
}
@Override
public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateBinaryStream");
}
@Override
public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateCharacterStream");
}
@Override
public void updateObject(int columnIndex, Object x, int scaleOrLength) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateObject");
}
@Override
public void updateObject(int columnIndex, Object x) throws SQLException {
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateObject");
}
@Override
public void updateObject(int columnIndex, Object x, SQLType targetSqlType, int scaleOrLength)
throws SQLException {
LOGGER.debug(
"public void updateObject(int columnIndex = {}, Object x = {}, SQLType targetSqlType = {}, int scaleOrLength = {})",
columnIndex,
x,
targetSqlType,
scaleOrLength);
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateObject(int columnIndex, Object x, SQLType targetSqlType, int scaleOrLength)");
}
@Override
public void updateObject(String columnLabel, Object x, SQLType targetSqlType, int scaleOrLength)
throws SQLException {
LOGGER.debug(
"public void updateObject(String columnLabel = {}, Object x = {}, SQLType targetSqlType = {}, int scaleOrLength = {})",
columnLabel,
x,
targetSqlType,
scaleOrLength);
checkIfClosed();
throw new DatabricksSQLFeatureNotSupportedException(
"Databricks JDBC has ResultSet as CONCUR_READ_ONLY. Doesn't support update function : updateObject(String columnLabel, Object x, SQLType targetSqlType, int scaleOrLength)");
}
public void updateObject(int columnIndex, Object x, SQLType targetSqlType) throws SQLException {
LOGGER.debug(
"public void updateObject(int columnIndex = {}, Object x = {}, SQLType targetSqlType = {})",