Skip to content

Commit cad2091

Browse files
committed
added jdbc api tests
1 parent 9bb959b commit cad2091

File tree

4 files changed

+296
-7
lines changed

4 files changed

+296
-7
lines changed

flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/UuidAvaticaParameterConverter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616
*/
1717
package org.apache.arrow.driver.jdbc.converter.impl;
1818

19+
import static org.apache.arrow.driver.jdbc.utils.SqlTypes.getSqlTypeIdFromArrowType;
20+
import static org.apache.arrow.driver.jdbc.utils.SqlTypes.getSqlTypeNameFromArrowType;
21+
1922
import java.nio.ByteBuffer;
20-
import java.sql.Types;
2123
import java.util.UUID;
2224
import org.apache.arrow.driver.jdbc.converter.AvaticaParameterConverter;
23-
import org.apache.arrow.driver.jdbc.utils.SqlTypes;
24-
import static org.apache.arrow.driver.jdbc.utils.SqlTypes.getSqlTypeIdFromArrowType;
25-
import static org.apache.arrow.driver.jdbc.utils.SqlTypes.getSqlTypeNameFromArrowType;
2625
import org.apache.arrow.vector.FieldVector;
2726
import org.apache.arrow.vector.UuidVector;
28-
import org.apache.arrow.vector.extension.UuidType;
29-
import org.apache.arrow.vector.types.pojo.ExtensionTypeRegistry;
3027
import org.apache.arrow.vector.types.pojo.Field;
3128
import org.apache.arrow.vector.util.UuidUtility;
3229
import org.apache.calcite.avatica.AvaticaParameter;

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

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@
2222
import static org.hamcrest.CoreMatchers.allOf;
2323
import static org.hamcrest.CoreMatchers.anyOf;
2424
import static org.hamcrest.CoreMatchers.containsString;
25+
import static org.hamcrest.CoreMatchers.equalTo;
2526
import static org.hamcrest.CoreMatchers.instanceOf;
2627
import static org.hamcrest.CoreMatchers.is;
28+
import static org.hamcrest.CoreMatchers.nullValue;
2729
import static org.hamcrest.MatcherAssert.assertThat;
2830
import static org.junit.jupiter.api.Assertions.*;
2931

3032
import com.google.common.collect.ImmutableSet;
3133
import java.nio.charset.StandardCharsets;
3234
import java.sql.Connection;
3335
import java.sql.DriverManager;
36+
import java.sql.PreparedStatement;
3437
import java.sql.ResultSet;
38+
import java.sql.ResultSetMetaData;
3539
import java.sql.SQLException;
3640
import java.sql.SQLTimeoutException;
3741
import java.sql.Statement;
@@ -42,6 +46,7 @@
4246
import java.util.List;
4347
import java.util.Random;
4448
import java.util.Set;
49+
import java.util.UUID;
4550
import java.util.concurrent.CountDownLatch;
4651
import org.apache.arrow.driver.jdbc.utils.CoreMockedSqlProducers;
4752
import org.apache.arrow.driver.jdbc.utils.FallbackFlightSqlProducer;
@@ -61,6 +66,7 @@
6166
import org.apache.arrow.vector.types.pojo.ArrowType;
6267
import org.apache.arrow.vector.types.pojo.Field;
6368
import org.apache.arrow.vector.types.pojo.Schema;
69+
import org.apache.arrow.vector.util.UuidUtility;
6470
import org.junit.jupiter.api.AfterAll;
6571
import org.junit.jupiter.api.BeforeAll;
6672
import org.junit.jupiter.api.Test;
@@ -795,4 +801,172 @@ public void testResultSetAppMetadata() throws Exception {
795801
"foo".getBytes(StandardCharsets.UTF_8));
796802
}
797803
}
804+
805+
@Test
806+
public void testSelectQueryWithUuidColumn() throws SQLException {
807+
// Expectations
808+
final int expectedRowCount = 4;
809+
final UUID[] expectedUuids = new UUID[] {
810+
CoreMockedSqlProducers.UUID_1,
811+
CoreMockedSqlProducers.UUID_2,
812+
CoreMockedSqlProducers.UUID_3,
813+
null
814+
};
815+
816+
final Integer[] expectedIds = new Integer[] {1, 2, 3, 4};
817+
818+
final List<UUID> actualUuids = new ArrayList<>(expectedRowCount);
819+
final List<Integer> actualIds = new ArrayList<>(expectedRowCount);
820+
821+
822+
// Query
823+
int actualRowCount = 0;
824+
try (Statement statement = connection.createStatement();
825+
ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.UUID_SQL_CMD)) {
826+
for (; resultSet.next(); actualRowCount++) {
827+
actualIds.add((Integer) resultSet.getObject("id"));
828+
actualUuids.add((UUID) resultSet.getObject("uuid_col"));
829+
}
830+
}
831+
832+
// Assertions
833+
int finalActualRowCount = actualRowCount;
834+
assertAll(
835+
"UUID ResultSet values are as expected",
836+
() -> assertThat(finalActualRowCount, is(equalTo(expectedRowCount))),
837+
() -> assertThat(actualIds.toArray(new Integer[0]), is(expectedIds)),
838+
() -> assertThat(actualUuids.toArray(new UUID[0]), is(expectedUuids)));
839+
}
840+
841+
@Test
842+
public void testGetObjectReturnsUuid() throws SQLException {
843+
try (Statement statement = connection.createStatement();
844+
ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.UUID_SQL_CMD)) {
845+
resultSet.next();
846+
Object result = resultSet.getObject("uuid_col");
847+
assertThat(result, instanceOf(UUID.class));
848+
assertThat(result, is(CoreMockedSqlProducers.UUID_1));
849+
}
850+
}
851+
852+
@Test
853+
public void testGetObjectByIndexReturnsUuid() throws SQLException {
854+
try (Statement statement = connection.createStatement();
855+
ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.UUID_SQL_CMD)) {
856+
resultSet.next();
857+
Object result = resultSet.getObject(2);
858+
assertThat(result, instanceOf(UUID.class));
859+
assertThat(result, is(CoreMockedSqlProducers.UUID_1));
860+
}
861+
}
862+
863+
@Test
864+
public void testGetStringReturnsHyphenatedFormat() throws SQLException {
865+
try (Statement statement = connection.createStatement();
866+
ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.UUID_SQL_CMD)) {
867+
resultSet.next();
868+
String result = resultSet.getString("uuid_col");
869+
assertThat(result, is(CoreMockedSqlProducers.UUID_1.toString()));
870+
}
871+
}
872+
873+
@Test
874+
public void testGetBytesReturns16ByteArray() throws SQLException {
875+
try (Statement statement = connection.createStatement();
876+
ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.UUID_SQL_CMD)) {
877+
resultSet.next();
878+
byte[] result = resultSet.getBytes("uuid_col");
879+
assertThat(result.length, is(16));
880+
assertThat(result, is(UuidUtility.getBytesFromUUID(CoreMockedSqlProducers.UUID_1)));
881+
}
882+
}
883+
884+
@Test
885+
public void testNullUuidHandling() throws SQLException {
886+
try (Statement statement = connection.createStatement();
887+
ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.UUID_SQL_CMD)) {
888+
// Skip to row 4 which has NULL UUID
889+
resultSet.next(); // row 1
890+
resultSet.next(); // row 2
891+
resultSet.next(); // row 3
892+
resultSet.next(); // row 4 (NULL UUID)
893+
894+
Object objResult = resultSet.getObject("uuid_col");
895+
assertThat(objResult, nullValue());
896+
assertThat(resultSet.wasNull(), is(true));
897+
898+
String strResult = resultSet.getString("uuid_col");
899+
assertThat(strResult, nullValue());
900+
assertThat(resultSet.wasNull(), is(true));
901+
902+
byte[] bytesResult = resultSet.getBytes("uuid_col");
903+
assertThat(bytesResult, nullValue());
904+
assertThat(resultSet.wasNull(), is(true));
905+
}
906+
}
907+
908+
@Test
909+
public void testMultipleUuidRows() throws SQLException {
910+
try (Statement statement = connection.createStatement();
911+
ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.UUID_SQL_CMD)) {
912+
resultSet.next();
913+
assertThat(resultSet.getObject("uuid_col"), is(CoreMockedSqlProducers.UUID_1));
914+
915+
resultSet.next();
916+
assertThat(resultSet.getObject("uuid_col"), is(CoreMockedSqlProducers.UUID_2));
917+
918+
resultSet.next();
919+
assertThat(resultSet.getObject("uuid_col"), is(CoreMockedSqlProducers.UUID_3));
920+
921+
resultSet.next();
922+
assertThat(resultSet.getObject("uuid_col"), nullValue());
923+
}
924+
}
925+
926+
@Test
927+
public void testUuidExtensionTypeInSchema() throws SQLException {
928+
try (Statement statement = connection.createStatement();
929+
ResultSet resultSet = statement.executeQuery(CoreMockedSqlProducers.UUID_SQL_CMD)) {
930+
ResultSetMetaData metaData = resultSet.getMetaData();
931+
932+
assertThat(metaData.getColumnCount(), is(2));
933+
assertThat(metaData.getColumnName(1), is("id"));
934+
assertThat(metaData.getColumnName(2), is("uuid_col"));
935+
936+
assertThat(metaData.getColumnType(2), is(java.sql.Types.OTHER));
937+
}
938+
}
939+
940+
@Test
941+
public void testPreparedStatementWithUuidParameter() throws SQLException {
942+
try (PreparedStatement pstmt =
943+
connection.prepareStatement(CoreMockedSqlProducers.UUID_PREPARED_SELECT_SQL_CMD)) {
944+
pstmt.setObject(1, CoreMockedSqlProducers.UUID_1);
945+
try (ResultSet rs = pstmt.executeQuery()) {
946+
rs.next();
947+
assertThat(rs.getObject("uuid_col"), is(CoreMockedSqlProducers.UUID_1));
948+
}
949+
}
950+
}
951+
952+
@Test
953+
public void testPreparedStatementWithUuidStringParameter() throws SQLException {
954+
try (PreparedStatement pstmt = connection.prepareStatement(CoreMockedSqlProducers.UUID_PREPARED_SELECT_SQL_CMD)) {
955+
pstmt.setString(1, CoreMockedSqlProducers.UUID_1.toString());
956+
try (ResultSet rs = pstmt.executeQuery()) {
957+
rs.next();
958+
assertThat(rs.getObject("uuid_col"), is(CoreMockedSqlProducers.UUID_1));
959+
}
960+
}
961+
}
962+
963+
@Test
964+
public void testPreparedStatementUpdateWithUuid() throws SQLException {
965+
try (PreparedStatement pstmt = connection.prepareStatement(CoreMockedSqlProducers.UUID_PREPARED_UPDATE_SQL_CMD)) {
966+
pstmt.setObject(1, CoreMockedSqlProducers.UUID_3);
967+
pstmt.setInt(2, 1);
968+
int updated = pstmt.executeUpdate();
969+
assertThat(updated, is(1));
970+
}
971+
}
798972
}

flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/converter/impl/UuidAvaticaParameterConverterTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.sql.Types;
2727
import java.util.UUID;
2828
import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestExtension;
29-
import org.apache.arrow.driver.jdbc.utils.SqlTypes;
3029
import org.apache.arrow.vector.UuidVector;
3130
import org.apache.arrow.vector.extension.UuidType;
3231
import org.apache.arrow.vector.types.pojo.Field;

0 commit comments

Comments
 (0)