-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.f90
62 lines (53 loc) · 1.57 KB
/
main.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
program main
use iso_c_binding
use libpq
implicit none
type(c_ptr) :: conn
type(c_ptr) :: res
character(256) :: values(3)
integer :: ntuples
integer :: i
conn = PQconnectdb("postgres://localhost/pgvector_fortran_test")
if (PQstatus(conn) /= CONNECTION_OK) then
stop 1
endif
res = PQexec(conn, "CREATE EXTENSION IF NOT EXISTS vector")
if (PQresultStatus(res) /= PGRES_COMMAND_OK) then
stop 1
endif
call PQclear(res)
res = PQexec(conn, "DROP TABLE IF EXISTS items")
if (PQresultStatus(res) /= PGRES_COMMAND_OK) then
stop 1
endif
call PQclear(res)
res = PQexec(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")
if (PQresultStatus(res) /= PGRES_COMMAND_OK) then
stop 1
endif
call PQclear(res)
values(1) = "[1,1,1]"
values(2) = "[2,2,2]"
values(3) = "[1,1,2]"
res = PQexecParams(conn, "INSERT INTO items (embedding) VALUES ($1), ($2), ($3)", 3, [0, 0, 0], values)
if (PQresultStatus(res) /= PGRES_COMMAND_OK) then
stop 1
endif
call PQclear(res)
values(1) = "[1,1,1]"
res = PQexecParams(conn, "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", 1, [0], values)
if (PQresultStatus(res) /= PGRES_TUPLES_OK) then
stop 1
endif
ntuples = PQntuples(res)
do i = 0, ntuples - 1
print *, PQgetvalue(res, i, 0), ": ", PQgetvalue(res, i, 1)
end do
call PQclear(res)
res = PQexec(conn, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
if (PQresultStatus(res) /= PGRES_COMMAND_OK) then
stop 1
endif
call PQclear(res)
call PQfinish(conn)
end program main