|
13 | 13 | import org.junit.jupiter.api.Test; |
14 | 14 |
|
15 | 15 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
16 | 17 | import static org.junit.jupiter.api.Assertions.assertNull; |
17 | 18 |
|
18 | 19 | public class JDBCJavaTest { |
@@ -117,6 +118,59 @@ void halfvecExample(boolean readBinary) throws SQLException { |
117 | 118 | assertNull(embeddings.get(3)); |
118 | 119 | } |
119 | 120 |
|
| 121 | + @Test |
| 122 | + void testBitReadText() throws SQLException { |
| 123 | + bitExample(false); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void testBitReadBinary() throws SQLException { |
| 128 | + bitExample(true); |
| 129 | + } |
| 130 | + |
| 131 | + void bitExample(boolean readBinary) throws SQLException { |
| 132 | + Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_java_test"); |
| 133 | + if (readBinary) { |
| 134 | + conn.unwrap(PGConnection.class).setPrepareThreshold(-1); |
| 135 | + } |
| 136 | + |
| 137 | + Statement setupStmt = conn.createStatement(); |
| 138 | + setupStmt.executeUpdate("CREATE EXTENSION IF NOT EXISTS vector"); |
| 139 | + setupStmt.executeUpdate("DROP TABLE IF EXISTS jdbc_items"); |
| 140 | + |
| 141 | + PGbit.addBitType(conn); |
| 142 | + |
| 143 | + Statement createStmt = conn.createStatement(); |
| 144 | + createStmt.executeUpdate("CREATE TABLE jdbc_items (id bigserial PRIMARY KEY, embedding bit(9))"); |
| 145 | + |
| 146 | + PreparedStatement insertStmt = conn.prepareStatement("INSERT INTO jdbc_items (embedding) VALUES (?), (?), (?), (?)"); |
| 147 | + insertStmt.setObject(1, new PGbit(new boolean[] {false, false, false, false, false, false, false, false, false})); |
| 148 | + insertStmt.setObject(2, new PGbit(new boolean[] {false, true, false, true, false, false, false, false, true})); |
| 149 | + insertStmt.setObject(3, new PGbit(new boolean[] {false, true, true, true, false, false, false, false, true})); |
| 150 | + insertStmt.setObject(4, null); |
| 151 | + insertStmt.executeUpdate(); |
| 152 | + |
| 153 | + PreparedStatement neighborStmt = conn.prepareStatement("SELECT * FROM jdbc_items ORDER BY embedding <~> ? LIMIT 5"); |
| 154 | + neighborStmt.setObject(1, new PGbit(new boolean[] {false, true, false, true, false, false, false, false, true})); |
| 155 | + ResultSet rs = neighborStmt.executeQuery(); |
| 156 | + List<Long> ids = new ArrayList<>(); |
| 157 | + List<PGbit> embeddings = new ArrayList<>(); |
| 158 | + while (rs.next()) { |
| 159 | + ids.add(rs.getLong("id")); |
| 160 | + embeddings.add((PGbit) rs.getObject("embedding")); |
| 161 | + } |
| 162 | + assertArrayEquals(new Long[] {2L, 3L, 1L, 4L}, ids.toArray()); |
| 163 | + assertEquals("010100001", embeddings.get(0).getValue()); |
| 164 | + assertEquals("011100001", embeddings.get(1).getValue()); |
| 165 | + assertEquals("000000000", embeddings.get(2).getValue()); |
| 166 | + assertNull(embeddings.get(3)); |
| 167 | + |
| 168 | + Statement indexStmt = conn.createStatement(); |
| 169 | + indexStmt.executeUpdate("CREATE INDEX ON jdbc_items USING ivfflat (embedding bit_hamming_ops) WITH (lists = 100)"); |
| 170 | + |
| 171 | + conn.close(); |
| 172 | + } |
| 173 | + |
120 | 174 | @Test |
121 | 175 | void testSparsevecReadText() throws SQLException { |
122 | 176 | sparsevecExample(false); |
|
0 commit comments