DEFAULT_PROPERTIES;
static {
@@ -131,6 +135,7 @@ public static PgConnectOptions fromEnv() {
private SslNegotiation sslNegotiation = DEFAULT_SSL_NEGOTIATION;
private ChannelBinding channelBinding = DEFAULT_CHANNEL_BINDING;
private boolean useLayer7Proxy = DEFAULT_USE_LAYER_7_PROXY;
+ private boolean vectorTypeEnabled = DEFAULT_VECTOR_TYPE_ENABLED;
public PgConnectOptions() {
super();
@@ -149,6 +154,7 @@ public PgConnectOptions(SqlConnectOptions other) {
sslMode = opts.sslMode;
sslNegotiation = opts.sslNegotiation;
channelBinding = opts.channelBinding;
+ vectorTypeEnabled = opts.vectorTypeEnabled;
}
}
@@ -158,6 +164,7 @@ public PgConnectOptions(PgConnectOptions other) {
sslMode = other.sslMode;
sslNegotiation = other.sslNegotiation;
channelBinding = other.channelBinding;
+ vectorTypeEnabled = other.vectorTypeEnabled;
}
@Override
@@ -304,6 +311,28 @@ public PgConnectOptions setUseLayer7Proxy(boolean useLayer7Proxy) {
return this;
}
+ /**
+ * @return whether the pgvector types ({@code vector}, {@code halfvec} and {@code sparsevec}) are enabled
+ */
+ public boolean isVectorTypeEnabled() {
+ return vectorTypeEnabled;
+ }
+
+ /**
+ * Enable the support of the pgvector types ({@code vector}, {@code halfvec} and {@code sparsevec}).
+ *
+ * When enabled, the object identifiers of the pgvector types are discovered with a query executed once
+ * when a connection is initialized. A pgvector type that is not found in the database is not available and
+ * a warning is logged, the connection is not otherwise affected.
+ *
+ * @param vectorTypeEnabled whether the pgvector types are enabled
+ * @return a reference to this, so the API can be used fluently
+ */
+ public PgConnectOptions setVectorTypeEnabled(boolean vectorTypeEnabled) {
+ this.vectorTypeEnabled = vectorTypeEnabled;
+ return this;
+ }
+
@Override
public PgConnectOptions setReconnectAttempts(int attempts) {
return (PgConnectOptions)super.setReconnectAttempts(attempts);
@@ -368,6 +397,7 @@ public boolean equals(Object o) {
if (sslMode != that.sslMode) return false;
if (sslNegotiation != that.sslNegotiation) return false;
if (channelBinding != that.channelBinding) return false;
+ if (vectorTypeEnabled != that.vectorTypeEnabled) return false;
return true;
}
@@ -379,6 +409,7 @@ public int hashCode() {
result = 31 * result + sslMode.hashCode();
result = 31 * result + sslNegotiation.hashCode();
result = 31 * result + channelBinding.hashCode();
+ result = 31 * result + (vectorTypeEnabled ? 1 : 0);
return result;
}
diff --git a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgConnectionUriParser.java b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgConnectionUriParser.java
index 32848693af..f327b591f1 100644
--- a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgConnectionUriParser.java
+++ b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgConnectionUriParser.java
@@ -187,6 +187,9 @@ private static void parseParameters(String parametersInfo, JsonObject configurat
case "channel_binding":
configuration.put("channelBinding", ChannelBinding.of(value));
break;
+ case "vector_type_enabled":
+ configuration.put("vectorTypeEnabled", parseBoolean(key, value));
+ break;
default:
properties.put(key, value);
break;
@@ -198,6 +201,15 @@ private static void parseParameters(String parametersInfo, JsonObject configurat
}
}
+ private static boolean parseBoolean(String key, String value) {
+ if (value.equalsIgnoreCase("true")) {
+ return true;
+ } else if (value.equalsIgnoreCase("false")) {
+ return false;
+ }
+ throw new IllegalArgumentException("The parameter " + key + " must be true or false");
+ }
+
private static void parseNetLocationValue(String hostValue, JsonObject configuration) {
if (isRegardedAsIpv6Address(hostValue)) {
configuration.put("host", hostValue.substring(1, hostValue.length() - 1));
diff --git a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgRow.java b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgRow.java
index 04a3ef7063..fca74f8176 100644
--- a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgRow.java
+++ b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgRow.java
@@ -21,7 +21,7 @@
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.pgclient.data.*;
-import io.vertx.sqlclient.data.Numeric;
+import io.vertx.sqlclient.data.*;
import io.vertx.sqlclient.impl.RowBase;
import io.vertx.sqlclient.internal.RowDescriptorBase;
@@ -152,6 +152,10 @@ public T get(Class type, int position) {
return type.cast(getCircle(position));
} else if (type == Box.class) {
return type.cast(getBox(position));
+ } else if (type == FloatVector.class || type == HalfFloatVector.class || type == SparseFloatVector.class) {
+ return type.cast(getValue(position));
+ } else if (type == BitVector.class) {
+ return type.cast(getBitVector(position));
} else if (type == JsonObject.class) {
return type.cast(getJson(position));
} else if (type == JsonArray.class) {
@@ -165,6 +169,24 @@ public T get(Class type, int position) {
throw new UnsupportedOperationException("Unsupported type " + type.getName());
}
+ /**
+ * Convert a {@code bit} / {@code varbit} value, a {@code String} when the value is decoded from the text format,
+ * a {@code Buffer} holding the {@code int32} bit length followed by the packed bits when the value is decoded
+ * from the binary format.
+ */
+ private BitVector getBitVector(int pos) {
+ Object val = getValue(pos);
+ if (val == null || val instanceof BitVector) {
+ return (BitVector) val;
+ } else if (val instanceof String) {
+ return BitVector.create((String) val);
+ } else if (val instanceof Buffer) {
+ Buffer buffer = (Buffer) val;
+ return BitVector.create(buffer.getBytes(4, buffer.length()), buffer.getInt(0));
+ }
+ throw new ClassCastException("Invalid BitVector value type " + val.getClass());
+ }
+
private Point getPoint(int pos) {
return (Point) getValue(pos);
}
diff --git a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgSocketConnection.java b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgSocketConnection.java
index ef6c518e3d..9245ee6832 100644
--- a/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgSocketConnection.java
+++ b/vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgSocketConnection.java
@@ -21,39 +21,49 @@
import io.netty.handler.codec.DecoderException;
import io.vertx.core.*;
import io.vertx.core.buffer.Buffer;
-import io.vertx.core.net.ClientSSLOptions;
-import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.core.internal.ContextInternal;
+import io.vertx.core.internal.logging.Logger;
+import io.vertx.core.internal.logging.LoggerFactory;
import io.vertx.core.internal.net.NetSocketInternal;
+import io.vertx.core.net.ClientSSLOptions;
+import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.pgclient.ChannelBinding;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgException;
-import io.vertx.pgclient.impl.codec.ExtendedQueryPgCommandMessage;
-import io.vertx.pgclient.impl.codec.NoticeResponse;
-import io.vertx.pgclient.impl.codec.PgCodec;
-import io.vertx.pgclient.impl.codec.PgCommandMessage;
-import io.vertx.pgclient.impl.codec.TxStatusEvent;
+import io.vertx.pgclient.impl.codec.*;
+import io.vertx.sqlclient.PropertyKind;
+import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.codec.CommandMessage;
import io.vertx.sqlclient.codec.SocketConnectionBase;
-import io.vertx.sqlclient.spi.connection.Connection;
import io.vertx.sqlclient.internal.PreparedStatement;
import io.vertx.sqlclient.internal.QueryResultHandler;
+import io.vertx.sqlclient.internal.RowDescriptorBase;
import io.vertx.sqlclient.spi.DatabaseMetadata;
-import io.vertx.sqlclient.spi.protocol.CommandBase;
-import io.vertx.sqlclient.spi.protocol.ExtendedQueryCommand;
-import io.vertx.sqlclient.spi.protocol.InitCommand;
-import io.vertx.sqlclient.spi.protocol.SavepointCommand;
-import io.vertx.sqlclient.spi.protocol.SimpleQueryCommand;
-import io.vertx.sqlclient.spi.protocol.TxCommand;
+import io.vertx.sqlclient.spi.connection.Connection;
+import io.vertx.sqlclient.spi.protocol.*;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
/**
* @author Julien Viet
*/
public class PgSocketConnection extends SocketConnectionBase {
+ private static final Logger logger = LoggerFactory.getLogger(PgSocketConnection.class);
+
+ private static final List VECTOR_TYPE_NAMES = List.of("vector", "halfvec", "sparsevec");
+
+ // Only the types that are members of the pgvector extension
+ private static final String VECTOR_OIDS_QUERY = "SELECT t.typname, t.oid FROM pg_type t " +
+ "JOIN pg_depend d ON d.classid = 'pg_type'::regclass AND d.objid = t.oid AND d.deptype = 'e' " +
+ "JOIN pg_extension e ON d.refclassid = 'pg_extension'::regclass AND e.oid = d.refobjid " +
+ "WHERE e.extname = 'vector' AND t.typname IN ('vector', 'halfvec', 'sparsevec')";
+
private PgCodec codec;
private final boolean useLayer7Proxy;
public int processId;
@@ -88,12 +98,65 @@ public void init() {
super.init();
}
- // TODO RETURN FUTURE ???
Future sendStartupMessage(String username, String password, String database, Map properties) {
InitCommand cmd = new InitCommand(this, username, password, database, properties);
Promise promise = context.promise();
schedule(cmd, promise);
- return promise.future();
+ Future future = promise.future();
+ if (connectOptions.isVectorTypeEnabled()) {
+ future = future.compose(conn -> discoverVectorOids().map(conn));
+ }
+ return future;
+ }
+
+ /**
+ * Discover the OIDs of the pgvector types, they are assigned when the extension is created.
+ * When the extension is not installed, no OID is found and the vector types are not available on this connection.
+ */
+ private Future discoverVectorOids() {
+ // An OID is an unsigned 32 bits integer, the simple query protocol returns it as text
+ Collector> collector = Collectors.toMap(row -> row.getString(0), row -> (int) Long.parseLong(row.getString(1)));
+ VectorOidsHandler handler = new VectorOidsHandler();
+ SimpleQueryCommand