Compare commits

...
6 Commits
Author SHA1 Message Date
Rob Bygrave d2a74ba6d1 #3701 Improve error message when missing dependency ebean-jackson-mapper 2025-11-23 20:17:39 +13:00
Rob BygraveandGitHub f7b4edb193 Merge pull request #3704 from ebean-orm/feature/bump-test-deps-bytebuddy
Bump test dependencies byte buddy and assertj
2025-11-20 09:35:09 +13:00
robin.bygrave 284c8d4d21 Bump test dependencies byte buddy and assertj 2025-11-20 09:23:39 +13:00
Rob BygraveandGitHub 59a5c5f9bd Bump dependency avaje-config to 4.2 (#3699) 2025-11-11 07:59:40 +13:00
Rob BygraveandGitHub 5b04d6eca3 #3551 Log warning for use of mapping column to Class (#3698)
I think it was a mistake for Ebean to support Class<?> from a security perspective. Instead, Ebean should just use a String <-> Varchar and leave if up to the application to take that String and convert it to a class [and then that potential Class initialisation is owned by the application code and all security considerations around that are owned by the application code].
2025-11-11 07:57:15 +13:00
d5547808a2 PGvector support (#3696)
* Add pgvector-types module.

* Add missing binder definitions.
Add missing PGbit type registration.
Few tests.

* Add cached bean test.

* Prefer final classes, minor formating only

---------

Co-authored-by: Rob Bygrave <robin.bygrave@gmail.com>
2025-11-09 16:55:51 +13:00
39 changed files with 1133 additions and 12 deletions
+85
View File
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>ebean-parent</artifactId>
<groupId>io.ebean</groupId>
<version>16.1.1</version>
<relativePath>../..</relativePath>
</parent>
<name>ebean-pgvector</name>
<description>ebean-pgvector composite</description>
<artifactId>ebean-pgvector</artifactId>
<properties>
<pgvector.version>0.1.6</pgvector.version>
<postgres.jdbc.version>42.7.2</postgres.jdbc.version>
</properties>
<dependencies>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-api</artifactId>
<version>16.1.1</version>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-core</artifactId>
<version>16.1.1</version>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-datasource</artifactId>
<version>${ebean-datasource.version}</version>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-migration</artifactId>
<version>${ebean-migration.version}</version>
</dependency>
<!-- Technically optional but most expected to use query beans -->
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-querybean</artifactId>
<version>16.1.1</version>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-platform-postgres</artifactId>
<version>16.1.1</version>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-pgvector-types</artifactId>
<version>16.1.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgres.jdbc.version}</version>
<exclusions>
<!-- exclude unnecessary checker framework -->
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.pgvector</groupId>
<artifactId>pgvector</artifactId>
<version>${pgvector.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,7 @@
package io.ebean.pgvector.assembly;
/**
* Nothing interesting here - required placeholder for javadoc.
*/
public class Assembly {
}
@@ -0,0 +1,9 @@
module io.ebean.pgvector {
requires transitive io.ebean.api;
requires transitive io.ebean.core;
requires transitive io.ebean.datasource;
requires transitive io.ebean.querybean;
requires transitive io.ebean.platform.postgres;
}
+1
View File
@@ -25,6 +25,7 @@
<module>ebean-postgres</module>
<module>ebean-postgis</module>
<module>ebean-net-postgis</module>
<module>ebean-pgvector</module>
<!-- <module>sqlanywhere</module>-->
<module>ebean-sqlite</module>
<module>ebean-sqlserver</module>
+1 -1
View File
@@ -35,7 +35,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-config</artifactId>
<version>4.1</version>
<version>4.2</version>
</dependency>
<dependency>
@@ -46,6 +46,11 @@ public class DbPlatformTypeMapping {
private static final DbPlatformType MULTILINESTRING = new DbPlatformType("multilinestring");
private static final DbPlatformType MULTIPOLYGON = new DbPlatformType("multipolygon");
private static final DbPlatformType VECTOR = new DbPlatformType("vector", 2000, null);
private static final DbPlatformType VECTOR_HALF = new DbPlatformType("halfvec", 4000, null);
private static final DbPlatformType VECTOR_BIT = new DbPlatformType("bit", 64000, null);
private static final DbPlatformType VECTOR_SPARSE = new DbPlatformType("sparsevec", 1000, null);
private final Map<DbType, DbPlatformType> typeMap = new EnumMap<>(DbType.class);
/**
@@ -93,6 +98,10 @@ public class DbPlatformTypeMapping {
put(DbType.MULTIPOINT, MULTIPOINT);
put(DbType.MULTILINESTRING, MULTILINESTRING);
put(DbType.MULTIPOLYGON, MULTIPOLYGON);
put(DbType.VECTOR, VECTOR);
put(DbType.VECTOR_HALF, VECTOR_HALF);
put(DbType.VECTOR_BIT, VECTOR_BIT);
put(DbType.VECTOR_SPARSE, VECTOR_SPARSE);
if (logicalTypes) {
// keep it logical for 2 layer DDL generation
@@ -51,7 +51,12 @@ public enum DbType {
JSONB(ExtraDbTypes.JSONB),
JSONCLOB(ExtraDbTypes.JSONClob),
JSONBLOB(ExtraDbTypes.JSONBlob),
JSONVARCHAR(ExtraDbTypes.JSONVarchar);
JSONVARCHAR(ExtraDbTypes.JSONVarchar),
VECTOR(ExtraDbTypes.VECTOR),
VECTOR_HALF(ExtraDbTypes.VECTOR_HALF),
VECTOR_BIT(ExtraDbTypes.VECTOR_BIT),
VECTOR_SPARSE(ExtraDbTypes.VECTOR_SPARSE);
private final int id;
@@ -74,4 +74,24 @@ public interface ExtraDbTypes {
*/
int MULTILINESTRING = 6007;
/**
* PGVector base type
*/
int VECTOR = 7000;
/**
* PGVector half precision float type
*/
int VECTOR_HALF = 7001;
/**
* PGVector binary type (bit)
*/
int VECTOR_BIT = 7002;
/**
* PGVector sparse type
*/
int VECTOR_SPARSE = 7003;
}
+12
View File
@@ -256,6 +256,18 @@
<version>16.1.1</version>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-pgvector</artifactId>
<version>16.1.1</version>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-pgvector-types</artifactId>
<version>16.1.1</version>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-sqlite</artifactId>
@@ -329,6 +329,13 @@ public final class Binder {
geoTypeBinder.bind(b, dataType, data);
break;
case DbPlatformType.VECTOR:
case DbPlatformType.VECTOR_HALF:
case DbPlatformType.VECTOR_BIT:
case DbPlatformType.VECTOR_SPARSE:
b.setObject(data);
break;
case java.sql.Types.OTHER:
b.setObject(data, dataType);
break;
@@ -216,6 +216,9 @@ public final class DefaultTypeManager implements TypeManager {
}
found = checkInheritedTypes(type);
}
if (found instanceof ScalarTypeClass) {
log.log(WARNING, "@Column mapping for type Class is deprecated. Please refer to https://ebean.io/docs/deprecated#class-mapping");
}
return found != ScalarTypeNotFound.INSTANCE ? found : null; // Do not return ScalarTypeNotFound, otherwise checks will fail
}
@@ -371,7 +374,7 @@ public final class DefaultTypeManager implements TypeManager {
private ScalarType<?> createJsonObjectMapperType(DeployBeanProperty prop, int dbType, DocPropertyType docType) {
if (jsonMapper == null) {
throw new IllegalArgumentException("Unsupported @DbJson mapping - Jackson ObjectMapper not present for " + prop);
throw new IllegalArgumentException("Unsupported @DbJson mapping - Missing dependency ebean-jackson-mapper? Jackson ObjectMapper not present for " + prop);
}
if (MutationDetection.DEFAULT == prop.getMutationDetection()) {
prop.setMutationDetection(jsonManager.mutationDetection());
@@ -10,7 +10,7 @@ import jakarta.persistence.PersistenceException;
@SuppressWarnings({"rawtypes"})
final class ScalarTypeClass extends ScalarTypeBaseVarchar<Class> {
public ScalarTypeClass() {
ScalarTypeClass() {
super(Class.class);
}
+1 -1
View File
@@ -69,7 +69,7 @@ module io.ebean.core {
exports io.ebeaninternal.server.querydefn to io.ebean.autotune, io.ebean.querybean, io.ebean.test, io.ebean.elastic;
exports io.ebeaninternal.server.rawsql to io.ebean.test;
exports io.ebeaninternal.server.json to io.ebean.test, io.ebean.elastic;
exports io.ebeaninternal.server.type to io.ebean.postgis, io.ebean.test, io.ebean.postgis.types;
exports io.ebeaninternal.server.type to io.ebean.postgis, io.ebean.test, io.ebean.postgis.types, io.ebean.pgvector;
exports io.ebeaninternal.server.transaction to io.ebean.test, io.ebean.elastic, io.ebean.spring.txn, io.ebean.k8scache;
exports io.ebeaninternal.server.util to io.ebean.querybean;
+17
View File
@@ -0,0 +1,17 @@
# editorconfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
spaces_around_operators = true
max_line_length = 130
[pom.xml]
# Because of <project... line
max_line_length = 999
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+3
View File
@@ -0,0 +1,3 @@
# ebean.postgis.types
Ebean support for PGvector types
+96
View File
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>ebean-parent</artifactId>
<groupId>io.ebean</groupId>
<version>16.1.1</version>
</parent>
<name>ebean pgvector types</name>
<artifactId>ebean-pgvector-types</artifactId>
<properties>
<pgvector.version>0.1.6</pgvector.version>
</properties>
<dependencies>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-platform-postgres</artifactId>
<version>16.1.1</version>
</dependency>
<!-- provided scope -->
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-core</artifactId>
<version>16.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.pgvector</groupId>
<artifactId>pgvector</artifactId>
<version>${pgvector.version}</version>
</dependency>
<!-- expected to be provided -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-test</artifactId>
<version>16.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.avaje.composite</groupId>
<artifactId>logback</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- for testing -->
<plugin>
<groupId>io.ebean</groupId>
<artifactId>ebean-maven-plugin</artifactId>
<version>${ebean-maven-plugin.version}</version>
<executions>
<execution>
<id>test</id>
<phase>process-test-classes</phase>
<configuration>
<transformArgs>debug=0</transformArgs>
</configuration>
<goals>
<goal>testEnhance</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,20 @@
package io.ebean.pgvector;
import io.ebean.DatabaseBuilder;
import io.ebean.core.type.ExtraTypeFactory;
import io.ebean.core.type.ScalarType;
import java.util.List;
public final class PGvectorExtraTypeFactory implements ExtraTypeFactory {
@Override
public List<? extends ScalarType<?>> createTypes(DatabaseBuilder.Settings config, Object objectMapper) {
return List.of(
new ScalarTypePGvector(),
new ScalarTypePGhalfvec(),
new ScalarTypePGsparsevec(),
new ScalarTypePGbit()
);
}
}
@@ -0,0 +1,21 @@
package io.ebean.pgvector;
import com.pgvector.PGbit;
import com.pgvector.PGvector;
import io.ebean.datasource.NewConnectionInitializer;
import java.sql.Connection;
import java.sql.SQLException;
public final class PGvectorNewConnectionInitializer implements NewConnectionInitializer {
@Override
public void preInitialize(Connection connection) {
try {
PGvector.registerTypes(connection);
PGbit.registerType(connection);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,96 @@
package io.ebean.pgvector;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.DocPropertyType;
import io.ebean.core.type.ScalarType;
import org.postgresql.util.PGobject;
import java.io.DataInput;
import java.io.DataOutput;
import java.sql.SQLException;
import java.sql.Types;
abstract class ScalarTypePGbase<T extends PGobject> implements ScalarType<T> {
private final int jdbcType;
private final Class<T> cls;
public ScalarTypePGbase(int jdbcType, Class<T> cls) {
this.jdbcType = jdbcType;
this.cls = cls;
}
@Override
public T read(DataReader reader) throws SQLException {
var obj=reader.getObject();
if(obj==null) return null;
return cls.cast(obj);
}
@Override
public void bind(DataBinder binder, T value) throws SQLException {
if(value==null) {
binder.setNull(Types.NULL);
} else {
binder.setObject(value);
}
}
@Override
public boolean jdbcNative() {
return true;
}
@Override
public int jdbcType() {
return jdbcType;
}
@Override
public Class<T> type() {
return cls;
}
@Override
public T readData(DataInput dataInput) {
return null;
}
@Override
public void writeData(DataOutput dataOutput, T v) {
}
@Override
public Object toJdbcType(Object value) {
return null;
}
@Override
public T toBeanType(Object value) {
return null;
}
@Override
public String formatValue(T value) {
return value.toString();
}
@Override
public DocPropertyType docType() {
return null;
}
@Override
public T jsonRead(JsonParser parser) {
return null;
}
@Override
public void jsonWrite(JsonGenerator writer, T value) {
}
}
@@ -0,0 +1,22 @@
package io.ebean.pgvector;
import com.pgvector.PGbit;
import io.ebean.config.dbplatform.ExtraDbTypes;
import java.sql.SQLException;
public final class ScalarTypePGbit extends ScalarTypePGbase<PGbit> {
public ScalarTypePGbit() {
super(ExtraDbTypes.VECTOR_BIT, PGbit.class);
}
@Override
public PGbit parse(String value) {
try {
return new PGbit(value);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
@@ -0,0 +1,22 @@
package io.ebean.pgvector;
import com.pgvector.PGhalfvec;
import io.ebean.config.dbplatform.ExtraDbTypes;
import java.sql.SQLException;
public final class ScalarTypePGhalfvec extends ScalarTypePGbase<PGhalfvec> {
public ScalarTypePGhalfvec() {
super(ExtraDbTypes.VECTOR_HALF, PGhalfvec.class);
}
@Override
public PGhalfvec parse(String value) {
try {
return new PGhalfvec(value);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
@@ -0,0 +1,22 @@
package io.ebean.pgvector;
import com.pgvector.PGsparsevec;
import io.ebean.config.dbplatform.ExtraDbTypes;
import java.sql.SQLException;
public final class ScalarTypePGsparsevec extends ScalarTypePGbase<PGsparsevec> {
public ScalarTypePGsparsevec() {
super(ExtraDbTypes.VECTOR_SPARSE, PGsparsevec.class);
}
@Override
public PGsparsevec parse(String value) {
try {
return new PGsparsevec(value);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
@@ -0,0 +1,20 @@
package io.ebean.pgvector;
import com.pgvector.PGvector;
import io.ebean.config.dbplatform.ExtraDbTypes;
public final class ScalarTypePGvector extends ScalarTypePGbase<PGvector> {
public ScalarTypePGvector() {
super(ExtraDbTypes.VECTOR, PGvector.class);
}
@Override
public PGvector parse(String value) {
try {
return new PGvector(value);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
@@ -0,0 +1 @@
io.ebean.pgvector.PGvectorExtraTypeFactory
@@ -0,0 +1 @@
io.ebean.pgvector.PGvectorNewConnectionInitializer
@@ -0,0 +1,20 @@
package org.example.domain;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
@MappedSuperclass
abstract class BaseEntity {
@Id
Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
@@ -0,0 +1,69 @@
package org.example.domain;
import com.pgvector.PGbit;
import com.pgvector.PGhalfvec;
import com.pgvector.PGsparsevec;
import com.pgvector.PGvector;
import io.ebean.annotation.Cache;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
@Entity
@Table(name="mybean_cached")
@Cache
public class CachedBean extends BaseEntity {
String name;
@Column(length = 800)
PGvector vector;
@Column(length = 200)
PGsparsevec sparsevec;
@Column(length = 200)
PGbit bit;
@Column(length = 200)
PGhalfvec halfvec;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public PGvector getVector() {
return vector;
}
public void setVector(PGvector vector) {
this.vector = vector;
}
public PGsparsevec getSparsevec() {
return sparsevec;
}
public void setSparsevec(PGsparsevec sparsevec) {
this.sparsevec = sparsevec;
}
public PGbit getBit() {
return bit;
}
public void setBit(PGbit bit) {
this.bit = bit;
}
public PGhalfvec getHalfvec() {
return halfvec;
}
public void setHalfvec(PGhalfvec halfvec) {
this.halfvec = halfvec;
}
}
@@ -0,0 +1,69 @@
package org.example.domain;
import com.pgvector.PGbit;
import com.pgvector.PGhalfvec;
import com.pgvector.PGsparsevec;
import com.pgvector.PGvector;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
@Entity
@Table(name="mybean")
public class MyBean extends BaseEntity {
String name;
@Column(length = 200)
PGvector vector;
@Column(length = 350)
PGsparsevec sparse;
@Column(length = 1200)
PGbit bit;
@Column(length = 420)
PGhalfvec halfvec;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public PGvector getVector() {
return vector;
}
public void setVector(PGvector vector) {
this.vector = vector;
}
public PGsparsevec getSparse() {
return sparse;
}
public void setSparse(PGsparsevec sparse) {
this.sparse = sparse;
}
public PGbit getBit() {
return bit;
}
public void setBit(PGbit bit) {
this.bit = bit;
}
public PGhalfvec getHalfvec() {
return halfvec;
}
public void setHalfvec(PGhalfvec halfvec) {
this.halfvec = halfvec;
}
}
@@ -0,0 +1,35 @@
package org.example.domain;
import com.pgvector.PGbit;
import com.pgvector.PGvector;
import io.ebean.DB;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class TestCachedBean {
@Test
void testCache() {
var v = new PGvector(TestInsertQuery.randomVector(800));
var b = new PGbit((TestInsertQuery.randomBitArray(200)));
CachedBean cb = new CachedBean();
cb.setName("test");
cb.setVector(v);
cb.setBit(b);
DB.insert(cb);
CachedBean r1 = DB.find(CachedBean.class, cb.getId());
assertNotNull(r1);
assertEquals(v, r1.getVector());
assertEquals(b, r1.getBit());
CachedBean r2 = DB.find(CachedBean.class, cb.getId());
assertNotNull(r2);
assertEquals(v, r2.getVector());
assertEquals(b, r2.getBit());
DB.delete(r2);
}
}
@@ -0,0 +1,136 @@
package org.example.domain;
import com.pgvector.PGbit;
import com.pgvector.PGhalfvec;
import com.pgvector.PGsparsevec;
import com.pgvector.PGvector;
import io.ebean.DB;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
class TestInsertQuery {
static float[] randomVector(int dim) {
Random rnd = new Random();
float[] vector = new float[dim];
for (int i = 0; i < dim; i++) {
vector[i] = rnd.nextFloat();
}
return vector;
}
static boolean compareHalfVectors(PGhalfvec v1, PGhalfvec v2) {
if (v1 == null || v2 == null) return v1 == v2;
float[] a1 = v1.toArray();
float[] a2 = v2.toArray();
if (a1.length != a2.length) return false;
for (int i = 0; i < a1.length; i++) {
if (Math.abs(a1[i] - a2[i]) > 0.001) return false;
}
return true;
}
static float[] randomSparseVector(int dim, int nonZeroCount) {
Random rnd = new Random();
float[] vector = new float[dim];
for (int i = 0; i < nonZeroCount; i++) {
int index;
do {
index = rnd.nextInt(dim);
} while (vector[index] != 0);
vector[index] = rnd.nextFloat();
}
return vector;
}
static boolean[] randomBitArray(int length) {
Random rnd = new Random();
boolean[] bits = new boolean[length];
for (int i = 0; i < length; i++) {
bits[i] = rnd.nextBoolean();
}
return bits;
}
@Test
public void insert() {
List<MyBean> list = DB.find(MyBean.class).findList();
for (MyBean MyBean : list) {
System.out.println(MyBean.getVector());
}
var v1 = new PGvector(randomVector(200));
MyBean myBean = new MyBean();
myBean.setName("test");
myBean.setVector(v1);
DB.save(myBean);
var dbBean = DB.find(MyBean.class, myBean.getId());
assertNotNull(dbBean);
assertEquals(myBean.getVector().toString(), dbBean.getVector().toString());
}
@Test
void differentTypes() {
var rv1 = new PGvector(randomVector(200));
var rv2 = new PGvector(randomVector(200));
var rh1 = new PGhalfvec(randomVector(420));
var rh2 = new PGhalfvec(randomVector(420));
var rb1 = new PGbit(randomBitArray(1200));
var rb2 = new PGbit(randomBitArray(1200));
var rs1 = new PGsparsevec(randomSparseVector(350, 2));
var rs2 = new PGsparsevec(randomSparseVector(350, 2));
MyBean b1 = new MyBean();
b1.setName("testTypes");
b1.setVector(rv1);
b1.setHalfvec(rh1);
b1.setBit(rb1);
b1.setSparse(rs1);
DB.save(b1);
MyBean b2 = new MyBean();
b2.setName("testTypes2");
b2.setVector(rv2);
b2.setHalfvec(rh2);
b2.setBit(rb2);
b2.setSparse(rs2);
DB.save(b2);
var f1 = DB.find(MyBean.class).where().eq("vector", rv1).findOne();
assertNotNull(f1);
assertEquals(b1.getId(), f1.getId());
assertEquals(b1.getVector(), f1.getVector());
assertTrue(compareHalfVectors(b1.getHalfvec(), f1.getHalfvec()));
assertEquals(b1.getBit(), f1.getBit());
assertEquals(b1.getSparse(), f1.getSparse());
var f2 = DB.find(MyBean.class).where().eq("sparse", rs2).findOne();
assertNotNull(f2);
assertEquals(b2.getId(), f2.getId());
assertEquals(b2.getVector(), f2.getVector());
assertTrue(compareHalfVectors(b2.getHalfvec(), f2.getHalfvec()));
assertEquals(b2.getBit(), f2.getBit());
assertEquals(b2.getSparse(), f2.getSparse());
var f3 = DB.find(MyBean.class).where().eq("bit", rb1).findOne();
assertNotNull(f3);
assertEquals(b1.getId(), f3.getId());
assertEquals(b1.getVector(), f3.getVector());
assertTrue(compareHalfVectors(b1.getHalfvec(), f3.getHalfvec()));
assertEquals(b1.getBit(), f3.getBit());
assertEquals(b1.getSparse(), f3.getSparse());
DB.delete(f1);
assertEquals(1, DB.find(MyBean.class).where().eq("halfvec", rh2).delete());
assertNull(DB.find(MyBean.class).where().eq("sparse", rs2).findOne());
assertNull(DB.find(MyBean.class).where().eq("bit", rb1).findOne());
}
}
@@ -0,0 +1,14 @@
ebean:
dbSchema: mypgvectorapp
test:
# useDocker: false
# shutdown: stop # stop | remove
platform: pgvector
ddlMode: dropCreate # none | dropCreate | create | migration | createOnly | migrationDropCreate
dbName: mypgvectorapp
pgvector:
containerName: ebeanbuild_pgvector
port: 8432
image: pgvector/pgvector:pg18
@@ -0,0 +1,23 @@
<configuration scan="true" scanPeriod="3 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
<!-- Logging for SQL etc -->
<logger name="io.ebean.docker" level="TRACE"/>
<logger name="io.ebean.SQL" level="TRACE"/>
<logger name="io.ebean.TXN" level="TRACE"/>
<logger name="io.ebean.SUM" level="TRACE"/>
<logger name="io.ebean.DDL" level="TRACE"/>
</configuration>
@@ -12,14 +12,13 @@ import org.postgis.Polygon;
import java.sql.SQLException;
import java.util.List;
public class TestInsertQuery {
class TestInsertQuery {
/**
* Not automated this test yet.
*/
@Test
public void insert() throws SQLException {
void insert() throws SQLException {
List<MyBean> list = DB.find(MyBean.class).findList();
for (MyBean MyBean : list) {
+2 -2
View File
@@ -12,8 +12,8 @@
<artifactId>ebean-test</artifactId>
<properties>
<bytebuddy.version>1.14.17</bytebuddy.version>
<assertj.version>3.26.0</assertj.version>
<bytebuddy.version>1.18.1</bytebuddy.version>
<assertj.version>3.27.6</assertj.version>
</properties>
<dependencies>
@@ -0,0 +1,49 @@
package io.ebean.test.config.platform;
import java.util.Properties;
final class PGvectorSetup implements PlatformSetup {
@Override
public Properties setup(Config config) {
int defaultPort = config.isUseDocker() ? 8432 : 5432;
config.setDockerPlatform("pgvector");
config.ddlMode("dropCreate");
config.setDefaultPort(defaultPort);
config.setUsernameDefault();
config.setPasswordDefault();
config.setUrl("jdbc:postgresql://${host}:${port}/${databaseName}");
String schema = config.getSchema();
if (schema != null && !schema.equals(config.getUsername())) {
config.urlAppend("?currentSchema=" + schema);
}
config.setDriver("org.postgresql.Driver");
config.datasourceDefaults();
return dockerProperties(config);
}
private Properties dockerProperties(Config config) {
if (!config.isUseDocker()) {
return new Properties();
}
config.setExtensions("vector");
config.setDockerContainerName("ut_pgvector");
config.setDockerVersion("pg18");
return config.getDockerProperties();
}
@Override
public void setupExtraDbDataSource(Config config) {
int defaultPort = config.isUseDocker() ? 8432 : 5432;
config.setDefaultPort(defaultPort);
config.setExtraUsernameDefault();
config.setExtraDbPasswordDefault();
config.setExtraUrl("jdbc:postgresql://${host}:${port}/${databaseName}");
config.extraDatasourceDefaults();
}
@Override
public boolean isLocal() {
return false;
}
}
@@ -26,6 +26,7 @@ public class PlatformAutoConfig {
KNOWN_PLATFORMS.put("sqlite", new SqliteSetup());
KNOWN_PLATFORMS.put("postgres", new PostgresSetup());
KNOWN_PLATFORMS.put("postgis", new PostgisSetup());
KNOWN_PLATFORMS.put("pgvector", new PGvectorSetup());
KNOWN_PLATFORMS.put("nuodb", new NuoDBSetup());
KNOWN_PLATFORMS.put("mysql", new MySqlSetup());
KNOWN_PLATFORMS.put("mariadb", new MariaDBSetup());
@@ -79,6 +79,11 @@ public class PostgresPlatform extends DatabasePlatform {
dbTypeMap.put(DbType.CLOB, dbTypeText);
dbTypeMap.put(DbType.LONGVARBINARY, dbBytea);
dbTypeMap.put(DbType.LONGVARCHAR, dbTypeText);
dbTypeMap.put(DbType.VECTOR, new DbPlatformType("vector", 512, 2000, null));
dbTypeMap.put(DbType.VECTOR_HALF, new DbPlatformType("halfvec", 512, 4000, null));
dbTypeMap.put(DbType.VECTOR_BIT, new DbPlatformType("bit", 512, 64000, null));
dbTypeMap.put(DbType.VECTOR_SPARSE, new DbPlatformType("sparsevec", 512, 64000, null));
}
@Override
+3 -2
View File
@@ -48,8 +48,8 @@
<ebean-ddl-runner.version>2.3</ebean-ddl-runner.version>
<ebean-migration-auto.version>1.2</ebean-migration-auto.version>
<ebean-migration.version>14.3.0</ebean-migration.version>
<ebean-test-containers.version>7.15</ebean-test-containers.version>
<ebean-datasource.version>10.1</ebean-datasource.version>
<ebean-test-containers.version>7.17</ebean-test-containers.version>
<ebean-datasource.version>10.2</ebean-datasource.version>
<ebean-agent.version>16.1.1</ebean-agent.version>
<ebean-maven-plugin.version>16.1.1</ebean-maven-plugin.version>
<surefire.useModulePath>false</surefire.useModulePath>
@@ -91,6 +91,7 @@
<module>ebean-querybean</module>
<module>ebean-postgis-types</module>
<module>ebean-net-postgis-types</module>
<module>ebean-pgvector-types</module>
<module>ebean-redis</module>
<module>platforms</module>
<module>composites</module>