forked from duckdb/duckdb-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuckDBDataChunkReader.java
More file actions
55 lines (46 loc) · 1.74 KB
/
DuckDBDataChunkReader.java
File metadata and controls
55 lines (46 loc) · 1.74 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
package org.duckdb;
import static org.duckdb.DuckDBBindings.*;
import java.nio.ByteBuffer;
import java.util.stream.LongStream;
import org.duckdb.DuckDBFunctions.FunctionException;
/**
* Reader over callback input data chunks.
*
* <p>Column index violations throw {@link IndexOutOfBoundsException}.
*/
public final class DuckDBDataChunkReader {
private final ByteBuffer chunkRef;
private final long rowCount;
private final long columnCount;
private final DuckDBReadableVector[] vectors;
DuckDBDataChunkReader(ByteBuffer chunkRef) {
if (chunkRef == null) {
throw new FunctionException("Invalid data chunk reference");
}
this.chunkRef = chunkRef;
this.rowCount = duckdb_data_chunk_get_size(chunkRef);
this.columnCount = duckdb_data_chunk_get_column_count(chunkRef);
this.vectors = new DuckDBReadableVector[Math.toIntExact(columnCount)];
for (long columnIndex = 0; columnIndex < columnCount; columnIndex++) {
ByteBuffer vectorRef = duckdb_data_chunk_get_vector(chunkRef, columnIndex);
int arrayIndex = Math.toIntExact(columnIndex);
vectors[arrayIndex] = new DuckDBReadableVector(vectorRef, rowCount);
}
}
public long rowCount() {
return rowCount;
}
public long columnCount() {
return columnCount;
}
public LongStream stream() {
return LongStream.range(0, rowCount);
}
public DuckDBReadableVector vector(long columnIndex) {
if (columnIndex < 0 || columnIndex >= columnCount) {
throw new IndexOutOfBoundsException("Column index out of bounds: " + columnIndex);
}
int arrayIndex = Math.toIntExact(columnIndex);
return vectors[arrayIndex];
}
}