Skip to content

Commit d36364a

Browse files
committed
fix returned ResultSet for Arrays
1 parent a602e6a commit d36364a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightJdbcArray.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.arrow.driver.jdbc.utils.SqlTypes;
2727
import org.apache.arrow.memory.util.LargeMemoryUtil;
2828
import org.apache.arrow.vector.FieldVector;
29+
import org.apache.arrow.vector.IntVector;
2930
import org.apache.arrow.vector.ValueVector;
3031
import org.apache.arrow.vector.VectorSchemaRoot;
3132
import org.apache.arrow.vector.types.pojo.ArrowType;
@@ -135,12 +136,22 @@ public ResultSet getResultSet(long index, int count) throws SQLException {
135136

136137
private static ResultSet getResultSetNoBoundariesCheck(
137138
ValueVector dataVector, long start, long count) throws SQLException {
139+
int intStart = LargeMemoryUtil.checkedCastToInt(start);
140+
int intCount = LargeMemoryUtil.checkedCastToInt(count);
141+
142+
// Create an index vector with 1-based indices (per JDBC spec) to return with value vector
143+
IntVector indexVector = new IntVector("INDEX", dataVector.getAllocator());
144+
indexVector.allocateNew(intCount);
145+
for (int i = 0; i < intCount; i++) {
146+
indexVector.set(i, i + 1);
147+
}
148+
indexVector.setValueCount(intCount);
149+
138150
TransferPair transferPair = dataVector.getTransferPair(dataVector.getAllocator());
139-
transferPair.splitAndTransfer(
140-
LargeMemoryUtil.checkedCastToInt(start), LargeMemoryUtil.checkedCastToInt(count));
141-
FieldVector vectorSlice = (FieldVector) transferPair.getTo();
151+
transferPair.splitAndTransfer(intStart, intCount);
152+
FieldVector valueVector = (FieldVector) transferPair.getTo();
142153

143-
VectorSchemaRoot vectorSchemaRoot = VectorSchemaRoot.of(vectorSlice);
154+
VectorSchemaRoot vectorSchemaRoot = VectorSchemaRoot.of(indexVector, valueVector);
144155
return ArrowFlightJdbcVectorSchemaRootResultSet.fromVectorSchemaRoot(vectorSchemaRoot);
145156
}
146157

flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/accessor/impl/complex/AbstractArrowFlightJdbcListAccessorTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.sql.Array;
2323
import java.sql.ResultSet;
24+
import java.sql.ResultSetMetaData;
2425
import java.util.Arrays;
2526
import java.util.List;
2627
import java.util.function.Supplier;
@@ -191,7 +192,12 @@ public void testShouldGetArrayGetResultSetReturnValidResultSet(
191192
try (ResultSet rs = array.getResultSet()) {
192193
int count = 0;
193194
while (rs.next()) {
194-
final int value = rs.getInt(1);
195+
// Column 1: 1-based index (per JDBC spec)
196+
final int index = rs.getInt(1);
197+
assertThat(index, equalTo(count + 1));
198+
199+
// Column 2: actual value (per JDBC spec)
200+
final int value = rs.getInt(2);
195201
assertThat(value, equalTo(currentRow * count));
196202
count++;
197203
}

0 commit comments

Comments
 (0)