Skip to content

Commit 8eaa0e7

Browse files
committed
Add new QueryBuilders for direct PostgreSQL querying
1 parent 6093ab5 commit 8eaa0e7

8 files changed

Lines changed: 938 additions & 0 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019-2026 The Polypheny Project
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a
7+
* copy of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.ddl;
26+
27+
import java.util.Map;
28+
import kong.unirest.core.HttpRequest;
29+
import org.apache.commons.lang3.tuple.ImmutablePair;
30+
import org.polypheny.simpleclient.query.Query;
31+
import org.polypheny.simpleclient.query.QueryBuilder;
32+
33+
34+
public class CreateBooleanFeatureIndex extends QueryBuilder {
35+
36+
private final String store;
37+
private final String method;
38+
private final String metric;
39+
private final int m;
40+
private final int efConstruction;
41+
private final int lists;
42+
43+
44+
public CreateBooleanFeatureIndex( String store, String method, String metric, int m, int efConstruction, int lists ) {
45+
this.store = store;
46+
this.method = method;
47+
this.metric = metric;
48+
this.m = m;
49+
this.efConstruction = efConstruction;
50+
this.lists = lists;
51+
}
52+
53+
54+
@Override
55+
public Query getNewQuery() {
56+
return new CreateBooleanFeatureIndexQuery( store, method, metric, m, efConstruction, lists );
57+
}
58+
59+
60+
private static class CreateBooleanFeatureIndexQuery extends Query {
61+
62+
private final String store;
63+
private final String method;
64+
private final String metric;
65+
private final int m;
66+
private final int efConstruction;
67+
private final int lists;
68+
private final boolean isHnsw;
69+
70+
71+
CreateBooleanFeatureIndexQuery( String store, String method, String metric, int m, int efConstruction, int lists ) {
72+
super( false );
73+
this.store = store;
74+
this.method = method;
75+
this.metric = metric;
76+
this.m = m;
77+
this.efConstruction = efConstruction;
78+
this.lists = lists;
79+
this.isHnsw = method.equals( "hnsw" );
80+
}
81+
82+
83+
@Override
84+
public String getSql() {
85+
String sql = "ALTER TABLE knn_booleanfeature ADD INDEX feature_bool_" + method
86+
+ " ON (feature) USING " + method;
87+
if ( store != null ) {
88+
sql += " ON STORE \"" + store + "\"";
89+
}
90+
String params = isHnsw
91+
? "m=" + m + ", ef_construction=" + efConstruction
92+
: "lists=" + lists;
93+
sql += " WITH (metric='" + metric + "', " + params + ")";
94+
return sql;
95+
}
96+
97+
98+
@Override
99+
public String getParameterizedSqlQuery() {
100+
return null;
101+
}
102+
103+
104+
@Override
105+
public Map<Integer, ImmutablePair<DataTypes, Object>> getParameterValues() {
106+
return null;
107+
}
108+
109+
110+
@Override
111+
public HttpRequest<?> getRest() {
112+
return null;
113+
}
114+
115+
116+
@Override
117+
public String getMongoQl() {
118+
return null;
119+
}
120+
121+
}
122+
123+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019-2026 The Polypheny Project
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a
7+
* copy of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.postgres.ddl;
26+
27+
import kong.unirest.core.HttpRequest;
28+
import org.apache.commons.lang3.tuple.ImmutablePair;
29+
import org.polypheny.simpleclient.query.Query;
30+
import org.polypheny.simpleclient.query.QueryBuilder;
31+
import java.util.Map;
32+
33+
public class PgCreateBooleanFeature extends QueryBuilder {
34+
private final int dimension;
35+
36+
37+
public PgCreateBooleanFeature( int dimension ) {
38+
this.dimension = dimension;
39+
}
40+
41+
42+
@Override
43+
public Query getNewQuery() {
44+
return new PgCreateBooleanFeatureQuery( dimension );
45+
}
46+
47+
48+
private static class PgCreateBooleanFeatureQuery extends Query {
49+
50+
private final int dimension;
51+
52+
53+
PgCreateBooleanFeatureQuery( int dimension ) {
54+
super( false );
55+
this.dimension = dimension;
56+
}
57+
58+
59+
@Override
60+
public String getSql() {
61+
return "CREATE TABLE knn_booleanfeature ("
62+
+ "id INTEGER NOT NULL, "
63+
+ "category VARCHAR(50), "
64+
+ "feature bit(" + dimension + ") NOT NULL, "
65+
+ "PRIMARY KEY (id))";
66+
}
67+
68+
69+
@Override
70+
public String getParameterizedSqlQuery() {
71+
return null;
72+
}
73+
74+
75+
@Override
76+
public Map<Integer, ImmutablePair<DataTypes, Object>> getParameterValues() {
77+
return null;
78+
}
79+
80+
81+
@Override
82+
public HttpRequest<?> getRest() {
83+
return null;
84+
}
85+
86+
87+
@Override
88+
public String getMongoQl() {
89+
return null;
90+
}
91+
92+
}
93+
94+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019-2026 The Polypheny Project
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a
7+
* copy of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.postgres.ddl;
26+
27+
import java.util.Map;
28+
import kong.unirest.core.HttpRequest;
29+
import org.apache.commons.lang3.tuple.ImmutablePair;
30+
import org.polypheny.simpleclient.query.Query;
31+
import org.polypheny.simpleclient.query.QueryBuilder;
32+
33+
34+
public class PgCreateBooleanFeatureIndex extends QueryBuilder {
35+
36+
private final String method;
37+
private final String opClass;
38+
private final int m;
39+
private final int efConstruction;
40+
private final int lists;
41+
42+
43+
public PgCreateBooleanFeatureIndex( String method, String metric, int m, int efConstruction, int lists ) {
44+
this.method = method;
45+
this.opClass = toOpClass( metric );
46+
this.m = m;
47+
this.efConstruction = efConstruction;
48+
this.lists = lists;
49+
}
50+
51+
52+
private static String toOpClass( String metric ) {
53+
return switch ( metric.toLowerCase() ) {
54+
case "hamming" -> "bit_hamming_ops";
55+
case "jaccard" -> "bit_jaccard_ops";
56+
default -> throw new IllegalArgumentException( "Provided boolean metric is invalid: " + metric );
57+
};
58+
}
59+
60+
61+
@Override
62+
public Query getNewQuery() {
63+
return new PgCreateBooleanFeatureIndexQuery( method, opClass, m, efConstruction, lists );
64+
}
65+
66+
67+
private static class PgCreateBooleanFeatureIndexQuery extends Query {
68+
69+
private final String method;
70+
private final String opClass;
71+
private final int m;
72+
private final int efConstruction;
73+
private final int lists;
74+
private final boolean isHnsw;
75+
76+
77+
PgCreateBooleanFeatureIndexQuery( String method, String opClass, int m, int efConstruction, int lists ) {
78+
super( false );
79+
this.method = method;
80+
this.opClass = opClass;
81+
this.m = m;
82+
this.efConstruction = efConstruction;
83+
this.lists = lists;
84+
this.isHnsw = method.equals( "hnsw" );
85+
}
86+
87+
88+
@Override
89+
public String getSql() {
90+
String withClause = isHnsw
91+
? "(m=" + m + ", ef_construction=" + efConstruction + ")"
92+
: "(lists=" + lists + ")";
93+
return "CREATE INDEX ON knn_booleanfeature USING " + method
94+
+ " (feature " + opClass + ") WITH " + withClause;
95+
}
96+
97+
98+
@Override
99+
public String getParameterizedSqlQuery() {
100+
return null;
101+
}
102+
103+
104+
@Override
105+
public Map<Integer, ImmutablePair<DataTypes, Object>> getParameterValues() {
106+
return null;
107+
}
108+
109+
110+
@Override
111+
public HttpRequest<?> getRest() {
112+
return null;
113+
}
114+
115+
116+
@Override
117+
public String getMongoQl() {
118+
return null;
119+
}
120+
121+
}
122+
123+
}

0 commit comments

Comments
 (0)