Skip to content

Commit fbb0c8a

Browse files
committed
Add test for Statement.execute() with protocol changes
1 parent 1de9682 commit fbb0c8a

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ArrowFlightStatementExecuteTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class ArrowFlightStatementExecuteTest {
6262
private static final String SAMPLE_LARGE_UPDATE_QUERY =
6363
"UPDATE this_large_table SET this_large_field = that_large_field FROM this_large_test WHERE this_large_condition";
6464
private static final long SAMPLE_LARGE_UPDATE_COUNT = Long.MAX_VALUE;
65+
private static final String SAMPLE_QUERY_CMD_V2 = "SELECT * FROM this_test_v2";
66+
private static final String SAMPLE_LARGE_UPDATE_QUERY_V2 =
67+
"UPDATE this_large_table_v2 SET this_large_field = that_large_field FROM this_large_test WHERE this_large_condition";
6568
private static final MockFlightSqlProducer PRODUCER = new MockFlightSqlProducer();
6669

6770
@RegisterExtension
@@ -96,6 +99,30 @@ public static void setUpBeforeClass() {
9699
}));
97100
PRODUCER.addUpdateQuery(SAMPLE_UPDATE_QUERY, SAMPLE_UPDATE_COUNT);
98101
PRODUCER.addUpdateQuery(SAMPLE_LARGE_UPDATE_QUERY, SAMPLE_LARGE_UPDATE_COUNT);
102+
103+
// V2 queries with is_update field set
104+
PRODUCER.addSelectQueryV2(
105+
SAMPLE_QUERY_CMD_V2,
106+
SAMPLE_QUERY_SCHEMA,
107+
Collections.singletonList(
108+
listener -> {
109+
try (final BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
110+
final VectorSchemaRoot root =
111+
VectorSchemaRoot.create(SAMPLE_QUERY_SCHEMA, allocator)) {
112+
final UInt1Vector vector = (UInt1Vector) root.getVector(VECTOR_NAME);
113+
IntStream.range(0, SAMPLE_QUERY_ROWS)
114+
.forEach(index -> vector.setSafe(index, index));
115+
vector.setValueCount(SAMPLE_QUERY_ROWS);
116+
root.setRowCount(SAMPLE_QUERY_ROWS);
117+
listener.start(root);
118+
listener.putNext();
119+
} catch (final Throwable throwable) {
120+
listener.error(throwable);
121+
} finally {
122+
listener.completed();
123+
}
124+
}));
125+
PRODUCER.addUpdateQueryV2(SAMPLE_LARGE_UPDATE_QUERY_V2, SAMPLE_LARGE_UPDATE_COUNT);
99126
}
100127

101128
@BeforeEach
@@ -168,4 +195,42 @@ public void testUpdateCountShouldStartOnZero() throws SQLException {
168195
is(allOf(equalTo(statement.getLargeUpdateCount()), equalTo(0L))));
169196
assertThat(statement.getResultSet(), is(nullValue()));
170197
}
198+
199+
@Test
200+
public void testExecuteShouldRunSelectQueryV2() throws SQLException {
201+
assertThat(statement.execute(SAMPLE_QUERY_CMD_V2), is(true));
202+
final Set<Byte> numbers =
203+
IntStream.range(0, SAMPLE_QUERY_ROWS)
204+
.boxed()
205+
.map(Integer::byteValue)
206+
.collect(Collectors.toCollection(HashSet::new));
207+
try (final ResultSet resultSet = statement.getResultSet()) {
208+
final int columnCount = resultSet.getMetaData().getColumnCount();
209+
assertThat(columnCount, is(1));
210+
int rowCount = 0;
211+
for (; resultSet.next(); rowCount++) {
212+
assertThat(numbers.remove(resultSet.getByte(1)), is(true));
213+
}
214+
assertThat(rowCount, is(equalTo(SAMPLE_QUERY_ROWS)));
215+
}
216+
assertThat(numbers, is(Collections.emptySet()));
217+
assertThat(
218+
(long) statement.getUpdateCount(),
219+
is(allOf(equalTo(statement.getLargeUpdateCount()), equalTo(-1L))));
220+
}
221+
222+
@Test
223+
public void testExecuteShouldRunUpdateQueryForLargeUpdateV2() throws SQLException {
224+
assertThat(statement.execute(SAMPLE_LARGE_UPDATE_QUERY_V2), is(false)); // UPDATE query.
225+
final long updateCountSmall = statement.getUpdateCount();
226+
final long updateCountLarge = statement.getLargeUpdateCount();
227+
assertThat(updateCountLarge, is(equalTo(SAMPLE_LARGE_UPDATE_COUNT)));
228+
assertThat(
229+
updateCountSmall,
230+
is(
231+
allOf(
232+
equalTo((long) AvaticaUtils.toSaturatedInt(updateCountLarge)),
233+
not(equalTo(updateCountLarge)))));
234+
assertThat(statement.getResultSet(), is(nullValue()));
235+
}
171236
}

0 commit comments

Comments
 (0)