Skip to content

Commit

Permalink
Started package
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Feb 16, 2025
1 parent 2efef24 commit 25d5dda
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0 (unreleased)

- First release
23 changes: 23 additions & 0 deletions Sources/Pgvector/HalfVector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if canImport(PostgresClientKit)
import PostgresClientKit
#endif

struct HalfVector: Equatable {
var value: [Float16]

init(_ value: [Float16]) {
self.value = value
}

static func == (lhs: HalfVector, rhs: HalfVector) -> Bool {
return lhs.value == rhs.value
}
}

#if canImport(PostgresClientKit)
extension HalfVector: PostgresValueConvertible {
public var postgresValue: PostgresValue {
return PostgresValue(String(describing: value))
}
}
#endif
Empty file removed Sources/Pgvector/Pgvector.swift
Empty file.
23 changes: 23 additions & 0 deletions Sources/Pgvector/Vector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if canImport(PostgresClientKit)
import PostgresClientKit
#endif

struct Vector: Equatable {
var value: [Float]

init(_ value: [Float]) {
self.value = value
}

static func == (lhs: Vector, rhs: Vector) -> Bool {
return lhs.value == rhs.value
}
}

#if canImport(PostgresClientKit)
extension Vector: PostgresValueConvertible {
public var postgresValue: PostgresValue {
return PostgresValue(String(describing: value))
}
}
#endif
14 changes: 14 additions & 0 deletions Tests/PgvectorTests/HalfVectorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation
import Testing
@testable import Pgvector

final class HalfVectorTests {
@Test func equatable() {
let a = HalfVector([1, 2, 3])
let b = HalfVector([1, 2, 3])
let c = HalfVector([1, 2, 4])
#expect(a == a)
#expect(a == b)
#expect(a != c)
}
}
5 changes: 3 additions & 2 deletions Tests/PgvectorTests/PostgresClientKitTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import PostgresClientKit
import Testing
@testable import Pgvector

final class PostgresClientKitTests {
@Test func example() throws {
Expand All @@ -26,11 +27,11 @@ final class PostgresClientKitTests {

text = "INSERT INTO items (embedding) VALUES ($1), ($2), ($3)"
statement = try connection.prepareStatement(text: text)
try statement.execute(parameterValues: ["[1,1,1]", "[2,2,2]", "[1,1,2]"])
try statement.execute(parameterValues: [Vector([1, 1, 1]), Vector([2, 2, 2]), Vector([1, 1, 2])])

text = "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5"
statement = try connection.prepareStatement(text: text)
let cursor = try statement.execute(parameterValues: ["[1,1,1]"])
let cursor = try statement.execute(parameterValues: [Vector([1, 1, 1])])

for row in cursor {
let columns = try row.get().columns
Expand Down
1 change: 1 addition & 0 deletions Tests/PgvectorTests/PostgresNIOTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import PostgresNIO
import Testing
@testable import Pgvector

final class PostgresNIOTests {
@Test func example() async throws {
Expand Down
14 changes: 14 additions & 0 deletions Tests/PgvectorTests/VectorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation
import Testing
@testable import Pgvector

final class VectorTests {
@Test func equatable() {
let a = Vector([1, 2, 3])
let b = Vector([1, 2, 3])
let c = Vector([1, 2, 4])
#expect(a == a)
#expect(a == b)
#expect(a != c)
}
}

0 comments on commit 25d5dda

Please sign in to comment.