Skip to content

Commit 44a960c

Browse files
committed
Add creation, insertion and queries for boolean vectors
1 parent 31cddf4 commit 44a960c

4 files changed

Lines changed: 475 additions & 0 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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;
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.Arrays;
32+
import java.util.Map;
33+
import java.util.Random;
34+
35+
public class SimpleKnnBooleanFeature extends QueryBuilder {
36+
37+
private static final boolean EXPECT_RESULT = true;
38+
private final int dimension;
39+
private final int limit;
40+
private final String norm; // Should be 'JACCARD' or 'HAMMING'
41+
private final Random random;
42+
43+
public SimpleKnnBooleanFeature( long randomSeed, int dimension, int limit, String norm ) {
44+
this.dimension = dimension;
45+
this.limit = limit;
46+
this.norm = norm;
47+
this.random = new Random( randomSeed );
48+
}
49+
50+
private Boolean[] getRandomVector() {
51+
Boolean[] booleans = new Boolean[this.dimension];
52+
for ( int i = 0; i < this.dimension; i++ ) {
53+
booleans[i] = random.nextBoolean();
54+
}
55+
return booleans;
56+
}
57+
58+
@Override
59+
public synchronized Query getNewQuery() {
60+
return new SimpleKnnBooleanFeatureQuery( getRandomVector(), limit, norm );
61+
}
62+
63+
private static class SimpleKnnBooleanFeatureQuery extends Query {
64+
65+
private final Boolean[] target;
66+
private final int limit;
67+
private final String norm;
68+
69+
public SimpleKnnBooleanFeatureQuery( Boolean[] target, int limit, String norm ) {
70+
super( EXPECT_RESULT );
71+
this.target = target;
72+
this.limit = limit;
73+
this.norm = norm;
74+
}
75+
76+
@Override
77+
public String getSql() {
78+
return "SELECT id, " + norm.toLowerCase() + "_distance(feature, ARRAY" + Arrays.toString( target ) + ") "
79+
+ "as dist " +
80+
"FROM knn_booleanfeature " + // Ensure this tableexists in DataGenerator
81+
"ORDER BY dist ASC LIMIT " + limit;
82+
}
83+
84+
85+
@Override
86+
public String getParameterizedSqlQuery() { return null; }
87+
88+
89+
@Override
90+
public Map<Integer, ImmutablePair<DataTypes, Object>>
91+
getParameterValues() { return null; }
92+
93+
94+
@Override
95+
public HttpRequest<?> getRest() { return null; }
96+
97+
98+
@Override
99+
public String getMongoQl() {
100+
return "db.knn_booleanfeature.aggregate([{" +
101+
" \"$vectorSearch\": {" +
102+
" \"path\": \"feature\"," +
103+
" \"queryVector\": " + Arrays.toString( target )
104+
+ "," +
105+
" \"metric\": \"" + norm + "\"," +
106+
" \"limit\": " + limit +
107+
" }" +
108+
"}])";
109+
}
110+
}
111+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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;
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.Arrays;
32+
import java.util.Map;
33+
import java.util.Random;
34+
35+
public class SimpleKnnBooleanFeatureFiltered extends QueryBuilder {
36+
37+
private static final boolean EXPECT_RESULT = true;
38+
private final int dimension;
39+
private final int limit;
40+
private final String norm;
41+
private final String filterCategory;
42+
private final Random random;
43+
44+
public SimpleKnnBooleanFeatureFiltered( long randomSeed, int dimension, int limit, String norm, String filterCategory ) {
45+
this.dimension = dimension;
46+
this.limit = limit;
47+
this.norm = norm;
48+
this.filterCategory = filterCategory;
49+
this.random = new Random( randomSeed );
50+
}
51+
52+
53+
private Boolean[] getRandomVector() {
54+
Boolean[] booleans = new Boolean[this.dimension];
55+
for ( int i = 0; i < this.dimension; i++ ) {
56+
booleans[i] = random.nextBoolean();
57+
}
58+
return booleans;
59+
}
60+
61+
62+
@Override
63+
public synchronized Query getNewQuery() {
64+
return new SimpleKnnBooleanFeatureFilteredQuery( getRandomVector(), limit, norm, filterCategory );
65+
}
66+
67+
68+
private static class SimpleKnnBooleanFeatureFilteredQuery extends Query {
69+
70+
private final Boolean[] target;
71+
private final int limit;
72+
private final String norm;
73+
private final String category;
74+
75+
public SimpleKnnBooleanFeatureFilteredQuery( Boolean[] target, int limit, String norm, String category ) {
76+
super( EXPECT_RESULT );
77+
this.target = target;
78+
this.limit = limit;
79+
this.norm = norm;
80+
this.category = category;
81+
}
82+
83+
84+
@Override
85+
public String getSql() {
86+
return "SELECT id, " + norm.toLowerCase() + "_distance(feature, ARRAY" + Arrays.toString( target ) + ") "
87+
+ "as dist " +
88+
"FROM knn_booleanfeature " +
89+
"WHERE category = '" + category + "' " +
90+
"ORDER BY dist ASC LIMIT " + limit;
91+
}
92+
93+
94+
@Override
95+
public String getParameterizedSqlQuery() { return null; }
96+
97+
98+
@Override
99+
public Map<Integer, ImmutablePair<DataTypes, Object>> getParameterValues() { return null; }
100+
101+
102+
@Override
103+
public HttpRequest<?> getRest() { return null; }
104+
105+
106+
@Override
107+
public String getMongoQl() {
108+
return "db.knn_booleanfeature.aggregate([{" +
109+
" \"$vectorSearch\": {" +
110+
" \"path\": \"feature\"," +
111+
" \"queryVector\": " + Arrays.toString( target ) + "," +
112+
" \"metric\": \"" + norm + "\"," +
113+
" \"limit\": " + limit + "," +
114+
" \"filter\": { \"category\": \"" + category + "\" }" +
115+
" }" +
116+
"}])";
117+
}
118+
}
119+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.creation;
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 CreateBooleanFeature extends QueryBuilder {
34+
35+
private final String store;
36+
private final int dimension;
37+
38+
39+
public CreateBooleanFeature( String store, int dimension ) {
40+
this.store = store;
41+
this.dimension = dimension;
42+
}
43+
44+
45+
@Override
46+
public Query getNewQuery() {
47+
return new CreateBooleanFeatureQuery( store, dimension );
48+
}
49+
50+
51+
private static class CreateBooleanFeatureQuery extends Query {
52+
53+
private final String store;
54+
private final int dimension;
55+
56+
57+
CreateBooleanFeatureQuery( String store, int dimension ) {
58+
super( false );
59+
this.store = store;
60+
this.dimension = dimension;
61+
}
62+
63+
64+
@Override
65+
public String getSql() {
66+
String sql = "CREATE TABLE knn_booleanfeature ("
67+
+ "id INTEGER NOT NULL, "
68+
+ "feature BOOLEAN NOT NULL ARRAY(1, " + this.dimension + "), "
69+
+ "category VARCHAR(50), "
70+
+ "PRIMARY KEY(id))";
71+
if ( this.store != null ) {
72+
sql += " ON STORE \"" + this.store + "\"";
73+
}
74+
return sql;
75+
}
76+
77+
78+
@Override
79+
public String getParameterizedSqlQuery() {
80+
return null;
81+
}
82+
83+
84+
@Override
85+
public Map<Integer, ImmutablePair<DataTypes, Object>> getParameterValues() {
86+
return null;
87+
}
88+
89+
90+
@Override
91+
public HttpRequest<?> getRest() {
92+
return null;
93+
}
94+
95+
96+
@Override
97+
public String getMongoQl() {
98+
return null;
99+
}
100+
101+
}
102+
103+
}

0 commit comments

Comments
 (0)