|
22 | 22 | import static org.hamcrest.CoreMatchers.allOf; |
23 | 23 | import static org.hamcrest.CoreMatchers.anyOf; |
24 | 24 | import static org.hamcrest.CoreMatchers.containsString; |
| 25 | +import static org.hamcrest.CoreMatchers.equalTo; |
25 | 26 | import static org.hamcrest.CoreMatchers.instanceOf; |
26 | 27 | import static org.hamcrest.CoreMatchers.is; |
| 28 | +import static org.hamcrest.CoreMatchers.nullValue; |
27 | 29 | import static org.hamcrest.MatcherAssert.assertThat; |
28 | 30 | import static org.junit.jupiter.api.Assertions.*; |
29 | 31 |
|
30 | 32 | import com.google.common.collect.ImmutableSet; |
31 | 33 | import java.nio.charset.StandardCharsets; |
32 | 34 | import java.sql.Connection; |
33 | 35 | import java.sql.DriverManager; |
| 36 | +import java.sql.PreparedStatement; |
34 | 37 | import java.sql.ResultSet; |
| 38 | +import java.sql.ResultSetMetaData; |
35 | 39 | import java.sql.SQLException; |
36 | 40 | import java.sql.SQLTimeoutException; |
37 | 41 | import java.sql.Statement; |
|
42 | 46 | import java.util.List; |
43 | 47 | import java.util.Random; |
44 | 48 | import java.util.Set; |
| 49 | +import java.util.UUID; |
45 | 50 | import java.util.concurrent.CountDownLatch; |
46 | 51 | import org.apache.arrow.driver.jdbc.utils.CoreMockedSqlProducers; |
47 | 52 | import org.apache.arrow.driver.jdbc.utils.FallbackFlightSqlProducer; |
|
61 | 66 | import org.apache.arrow.vector.types.pojo.ArrowType; |
62 | 67 | import org.apache.arrow.vector.types.pojo.Field; |
63 | 68 | import org.apache.arrow.vector.types.pojo.Schema; |
| 69 | +import org.apache.arrow.vector.util.UuidUtility; |
64 | 70 | import org.junit.jupiter.api.AfterAll; |
65 | 71 | import org.junit.jupiter.api.BeforeAll; |
66 | 72 | import org.junit.jupiter.api.Test; |
@@ -795,4 +801,172 @@ public void testResultSetAppMetadata() throws Exception { |
795 | 801 | "foo".getBytes(StandardCharsets.UTF_8)); |
796 | 802 | } |
797 | 803 | } |
| 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 | + } |
798 | 972 | } |
0 commit comments