forked from databricks/databricks-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrowResultChunkTest.java
More file actions
264 lines (249 loc) · 10.4 KB
/
Copy pathArrowResultChunkTest.java
File metadata and controls
264 lines (249 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.databricks.jdbc.api.impl.arrow;
import static com.databricks.jdbc.TestConstants.*;
import static java.lang.Math.min;
import static org.junit.jupiter.api.Assertions.*;
import com.databricks.jdbc.exception.DatabricksParsingException;
import com.databricks.jdbc.exception.DatabricksSQLException;
import com.databricks.jdbc.model.client.thrift.generated.TSparkArrowResultLink;
import com.databricks.jdbc.model.core.ColumnInfo;
import com.databricks.jdbc.model.core.ColumnInfoTypeName;
import com.databricks.sdk.service.sql.BaseChunkInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.*;
import org.apache.arrow.vector.dictionary.DictionaryProvider;
import org.apache.arrow.vector.ipc.ArrowStreamWriter;
import org.apache.arrow.vector.ipc.ArrowWriter;
import org.apache.arrow.vector.types.Types;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
import org.apache.arrow.vector.types.pojo.Schema;
import org.junit.jupiter.api.Test;
public class ArrowResultChunkTest {
private final Random random = new Random();
private final int rowsInRecordBatch = 20;
private final long totalRows = 110;
@Test
public void testReleaseUnusedChunk() throws Exception {
// Arrange
BaseChunkInfo chunkInfo =
new BaseChunkInfo()
.setChunkIndex(0L)
.setByteCount(200L)
.setRowOffset(0L)
.setRowCount(totalRows);
ArrowResultChunk arrowResultChunk =
ArrowResultChunk.builder()
.withStatementId(TEST_STATEMENT_ID)
.withChunkInfo(chunkInfo)
.build();
// Assert
assert (arrowResultChunk.getRecordBatchCountInChunk() == 0);
arrowResultChunk.releaseChunk();
}
@Test
public void testGetArrowDataFromInputStream() throws Exception {
// Arrange
BaseChunkInfo chunkInfo =
new BaseChunkInfo()
.setChunkIndex(0L)
.setByteCount(200L)
.setRowOffset(0L)
.setRowCount(totalRows);
ArrowResultChunk arrowResultChunk =
ArrowResultChunk.builder()
.withStatementId(TEST_STATEMENT_ID)
.withChunkInfo(chunkInfo)
.withChunkStatus(ChunkStatus.PROCESSING_SUCCEEDED)
.build();
Schema schema = createTestSchema();
Object[][] testData = createTestData(schema, (int) totalRows);
File arrowFile =
createTestArrowFile("TestFile", schema, testData, new RootAllocator(Integer.MAX_VALUE));
// Act
arrowResultChunk.initializeData(new FileInputStream(arrowFile));
// Assert
int totalRecordBatches = (int) ((totalRows + rowsInRecordBatch) / rowsInRecordBatch);
assertEquals(arrowResultChunk.getRecordBatchCountInChunk(), totalRecordBatches);
arrowResultChunk.releaseChunk();
arrowResultChunk.releaseChunk(); // calling it a second time also does not throw error.
}
@Test
public void testGetArrowDataFromThriftInput() throws DatabricksParsingException {
TSparkArrowResultLink chunkInfo =
new TSparkArrowResultLink()
.setRowCount(totalRows)
.setFileLink(TEST_STRING)
.setExpiryTime(1000)
.setBytesNum(200L);
ArrowResultChunk arrowResultChunk =
ArrowResultChunk.builder()
.withStatementId(TEST_STATEMENT_ID)
.withThriftChunkInfo(0, chunkInfo)
.build();
assertNull(arrowResultChunk.errorMessage);
assertEquals(arrowResultChunk.chunkLink.getExternalLink(), TEST_STRING);
assertEquals(arrowResultChunk.getChunkIndex(), 0);
}
private File createTestArrowFile(
String fileName, Schema schema, Object[][] testData, RootAllocator allocator)
throws IOException {
File file = new File(fileName);
int cols = testData.length;
int rows = testData[0].length;
VectorSchemaRoot vectorSchemaRoot = VectorSchemaRoot.create(schema, allocator);
ArrowWriter writer =
new ArrowStreamWriter(
vectorSchemaRoot,
new DictionaryProvider.MapDictionaryProvider(),
new FileOutputStream(file));
writer.start();
for (int j = 0; j < rows; j += rowsInRecordBatch) {
int rowsToAddToRecordBatch = min(rowsInRecordBatch, rows - j);
vectorSchemaRoot.setRowCount(rowsToAddToRecordBatch);
for (int i = 0; i < cols; i++) {
Types.MinorType type = Types.getMinorTypeForArrowType(schema.getFields().get(i).getType());
FieldVector fieldVector = vectorSchemaRoot.getFieldVectors().get(i);
if (type.equals(Types.MinorType.INT)) {
IntVector intVector = (IntVector) fieldVector;
intVector.setInitialCapacity(rowsToAddToRecordBatch);
for (int k = 0; k < rowsToAddToRecordBatch; k++) {
intVector.set(k, 1, (int) testData[i][j + k]);
}
} else if (type.equals(Types.MinorType.FLOAT8)) {
Float8Vector float8Vector = (Float8Vector) fieldVector;
float8Vector.setInitialCapacity(rowsToAddToRecordBatch);
for (int k = 0; k < rowsToAddToRecordBatch; k++) {
float8Vector.set(k, 1, (double) testData[i][j + k]);
}
}
fieldVector.setValueCount(rowsToAddToRecordBatch);
}
writer.writeBatch();
}
return file;
}
private Schema createTestSchema() {
List<Field> fieldList = new ArrayList<>();
FieldType fieldType1 = new FieldType(false, Types.MinorType.INT.getType(), null);
FieldType fieldType2 = new FieldType(false, Types.MinorType.FLOAT8.getType(), null);
fieldList.add(new Field("Field1", fieldType1, null));
fieldList.add(new Field("Field2", fieldType2, null));
return new Schema(fieldList);
}
private Object[][] createTestData(Schema schema, int rows) {
int cols = schema.getFields().size();
Object[][] data = new Object[cols][rows];
for (int i = 0; i < cols; i++) {
Types.MinorType type = Types.getMinorTypeForArrowType(schema.getFields().get(i).getType());
if (type.equals(Types.MinorType.INT)) {
for (int j = 0; j < rows; j++) {
data[i][j] = random.nextInt();
}
} else if (type.equals(Types.MinorType.FLOAT8)) {
for (int j = 0; j < rows; j++) {
data[i][j] = random.nextDouble();
}
}
}
return data;
}
@Test
public void testHasNextRow() throws DatabricksSQLException {
BaseChunkInfo emptyChunkInfo =
new BaseChunkInfo().setChunkIndex(0L).setByteCount(200L).setRowOffset(0L).setRowCount(0L);
ArrowResultChunk arrowResultChunk =
ArrowResultChunk.builder()
.withStatementId(TEST_STATEMENT_ID)
.withChunkInfo(emptyChunkInfo)
.withChunkStatus(ChunkStatus.PROCESSING_SUCCEEDED)
.build();
arrowResultChunk.recordBatchList = Collections.nCopies(3, new ArrayList<>());
assertFalse(arrowResultChunk.getChunkIterator().hasNextRow());
BaseChunkInfo chunkInfo =
new BaseChunkInfo().setChunkIndex(18L).setByteCount(200L).setRowOffset(0L).setRowCount(4L);
arrowResultChunk =
ArrowResultChunk.builder()
.withStatementId(TEST_STATEMENT_ID)
.withChunkInfo(chunkInfo)
.withChunkStatus(ChunkStatus.PROCESSING_SUCCEEDED)
.build();
int size = 2;
IntVector dummyVector = new IntVector("dummy_vector", new RootAllocator());
dummyVector.allocateNew(size);
dummyVector.setValueCount(size);
for (int i = 0; i < size; i++) {
dummyVector.set(i, i * 10);
}
arrowResultChunk.recordBatchList =
List.of(List.of(dummyVector), List.of(dummyVector), new ArrayList<>());
ArrowResultChunkIterator iterator = arrowResultChunk.getChunkIterator();
ColumnInfo intColumnInfo = new ColumnInfo();
assertTrue(iterator.hasNextRow());
iterator.nextRow();
assertEquals(
0, iterator.getColumnObjectAtCurrentRow(0, ColumnInfoTypeName.INT, "INT", intColumnInfo));
assertTrue(iterator.hasNextRow());
iterator.nextRow();
assertEquals(
10, iterator.getColumnObjectAtCurrentRow(0, ColumnInfoTypeName.INT, "INT", intColumnInfo));
assertTrue(iterator.hasNextRow());
iterator.nextRow();
assertEquals(
0, iterator.getColumnObjectAtCurrentRow(0, ColumnInfoTypeName.INT, "INT", intColumnInfo));
assertTrue(iterator.hasNextRow());
iterator.nextRow();
assertEquals(
10, iterator.getColumnObjectAtCurrentRow(0, ColumnInfoTypeName.INT, "INT", intColumnInfo));
assertFalse(iterator.hasNextRow());
}
@Test
public void testEmptyRecordBatches() throws DatabricksSQLException {
BaseChunkInfo chunkInfo =
new BaseChunkInfo().setChunkIndex(18L).setByteCount(200L).setRowOffset(0L).setRowCount(4L);
ArrowResultChunk arrowResultChunk =
ArrowResultChunk.builder()
.withStatementId(TEST_STATEMENT_ID)
.withChunkInfo(chunkInfo)
.withChunkStatus(ChunkStatus.PROCESSING_SUCCEEDED)
.build();
int size = 2;
IntVector dummyVector = new IntVector("dummy_vector", new RootAllocator());
dummyVector.allocateNew(size);
dummyVector.setValueCount(size);
for (int i = 0; i < size; i++) {
dummyVector.set(i, i * 10);
}
IntVector emptyVector = new IntVector("empty_vector", new RootAllocator());
emptyVector.allocateNew(0);
emptyVector.setValueCount(0);
arrowResultChunk.recordBatchList =
List.of(List.of(dummyVector), List.of(emptyVector), List.of(dummyVector));
ColumnInfo intColumnInfo = new ColumnInfo();
ArrowResultChunkIterator iterator = arrowResultChunk.getChunkIterator();
assertTrue(iterator.hasNextRow());
iterator.nextRow();
assertEquals(
0, iterator.getColumnObjectAtCurrentRow(0, ColumnInfoTypeName.INT, "INT", intColumnInfo));
assertTrue(iterator.hasNextRow());
iterator.nextRow();
assertEquals(
10, iterator.getColumnObjectAtCurrentRow(0, ColumnInfoTypeName.INT, "INT", intColumnInfo));
assertTrue(iterator.hasNextRow());
iterator.nextRow();
assertEquals(
0, iterator.getColumnObjectAtCurrentRow(0, ColumnInfoTypeName.INT, "INT", intColumnInfo));
assertTrue(iterator.hasNextRow());
iterator.nextRow();
assertEquals(
10, iterator.getColumnObjectAtCurrentRow(0, ColumnInfoTypeName.INT, "INT", intColumnInfo));
assertFalse(iterator.hasNextRow());
}
}