Skip to content

Commit 31cddf4

Browse files
committed
Add new query types containing distance cross-joins
1 parent d247424 commit 31cddf4

9 files changed

Lines changed: 282 additions & 12 deletions

File tree

src/main/java/org/polypheny/simpleclient/scenario/vectorbench/VectorBench.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.CreateMetadata;
4646
import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.CreateRealFeature;
4747
import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.MetadataKnnIntFeature;
48+
import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.MetadataKnnRealCrossJoin;
4849
import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.MetadataKnnRealFeature;
4950
import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.SimpleKnnIdRealFeature;
5051
import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.SimpleKnnIntFeature;
52+
import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.SimpleKnnRealCrossJoin;
5153
import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.SimpleKnnRealFeature;
5254
import org.polypheny.simpleclient.scenario.vectorbench.queryBuilder.SimpleMetadata;
5355

@@ -122,6 +124,8 @@ public long execute( ProgressReporter progressReporter, CsvWriter csvWriter, Fil
122124
addNumberOfTimes( queryList, new SimpleKnnIdRealFeature( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm ), config.numberOfSimpleKnnIdRealFeatureQueries );
123125
addNumberOfTimes( queryList, new MetadataKnnIntFeature( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm ), config.numberOfMetadataKnnIntFeatureQueries );
124126
addNumberOfTimes( queryList, new MetadataKnnRealFeature( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm ), config.numberOfMetadataKnnRealFeatureQueries );
127+
addNumberOfTimes( queryList, new SimpleKnnRealCrossJoin( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm ), config.numberOfSimpleKnnRealCrossJoinQueries );
128+
addNumberOfTimes( queryList, new MetadataKnnRealCrossJoin( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm ), config.numberOfMetadataKnnRealCrossJoinQueries );
125129

126130
return commonExecute( queryList, progressReporter, outputDirectory, numberOfThreads, Query::getSql, () -> executorFactory.createExecutorInstance( csvWriter ), new Random() );
127131
}
@@ -139,6 +143,9 @@ public void warmUp( ProgressReporter progressReporter ) {
139143
SimpleKnnIdRealFeature simpleKnnIdRealFeatureBuilder = new SimpleKnnIdRealFeature( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm );
140144
MetadataKnnIntFeature metadataKnnIntFeature = new MetadataKnnIntFeature( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm );
141145
MetadataKnnRealFeature metadataKnnRealFeature = new MetadataKnnRealFeature( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm );
146+
MetadataKnnRealCrossJoin metadataKnnCrossJoin = new MetadataKnnRealCrossJoin( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm );
147+
SimpleKnnRealCrossJoin simpleKnnCrossJoin = new SimpleKnnRealCrossJoin( config.randomSeedQuery, config.dimensionFeatureVectors, config.limitKnnQueries, config.distanceNorm );
148+
142149

143150
for ( int i = 0; i < config.numberOfWarmUpIterations; i++ ) {
144151
try {
@@ -166,6 +173,12 @@ public void warmUp( ProgressReporter progressReporter ) {
166173
if ( config.numberOfMetadataKnnRealFeatureQueries > 0 ) {
167174
executor.executeQuery( metadataKnnRealFeature.getNewQuery() );
168175
}
176+
if ( config.numberOfMetadataKnnRealCrossJoinQueries > 0 ) {
177+
executor.executeQuery( metadataKnnCrossJoin.getNewQuery() );
178+
}
179+
if ( config.numberOfSimpleKnnRealCrossJoinQueries > 0 ) {
180+
executor.executeQuery( simpleKnnCrossJoin.getNewQuery() );
181+
}
169182
} catch ( ExecutorException e ) {
170183
throw new RuntimeException( "Error while executing warm-up queries", e );
171184
} finally {

src/main/java/org/polypheny/simpleclient/scenario/vectorbench/VectorBenchConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class VectorBenchConfig extends AbstractConfig {
5353
public int numberOfSimpleKnnIdRealFeatureQueries;
5454
public int numberOfMetadataKnnIntFeatureQueries;
5555
public int numberOfMetadataKnnRealFeatureQueries;
56+
public int numberOfSimpleKnnRealCrossJoinQueries;
57+
public int numberOfMetadataKnnRealCrossJoinQueries;
5658
// public final int numberOfCombinedQueries;
5759

5860
public int limitKnnQueries;
@@ -94,6 +96,8 @@ public VectorBenchConfig(Properties properties, int multiplier ) {
9496
numberOfSimpleKnnIdRealFeatureQueries = getIntProperty( properties, "numberOfSimpleKnnIdRealFeatureQueries" ) * multiplier;
9597
numberOfMetadataKnnIntFeatureQueries = getIntProperty( properties, "numberOfMetadataKnnIntFeatureQueries" ) * multiplier;
9698
numberOfMetadataKnnRealFeatureQueries = getIntProperty( properties, "numberOfMetadataKnnRealFeatureQueries" ) * multiplier;
99+
numberOfSimpleKnnRealCrossJoinQueries = getIntProperty( properties, "numberOfSimpleKnnRealCrossJoinQueries" ) * multiplier;
100+
numberOfMetadataKnnRealCrossJoinQueries = getIntProperty( properties, "numberOfMetadataKnnRealCrossJoinQueries" ) * multiplier;
97101
limitKnnQueries = getIntProperty( properties, "limitKnnQueries" );
98102
distanceNorm = getStringProperty( properties, "distanceNorm" );
99103
}
@@ -132,6 +136,8 @@ public VectorBenchConfig(Map<String, String> cdl ) {
132136
numberOfSimpleKnnIdRealFeatureQueries = Integer.parseInt( cdl.get( "numberOfSimpleKnnIdRealFeatureQueries" ) );
133137
numberOfMetadataKnnIntFeatureQueries = Integer.parseInt( cdl.get( "numberOfMetadataKnnIntFeatureQueries" ) );
134138
numberOfMetadataKnnRealFeatureQueries = Integer.parseInt( cdl.get( "numberOfMetadataKnnRealFeatureQueries" ) );
139+
numberOfSimpleKnnRealCrossJoinQueries = Integer.parseInt( cdl.get( "numberOfSimpleKnnRealCrossJoinQueries" ) );
140+
numberOfMetadataKnnRealCrossJoinQueries = Integer.parseInt( cdl.get( "numberOfMetadataKnnRealCrossJoinQueries" ) );
135141
// numberOfCombinedQueries = getIntProperty( properties, "numberOfCombinedQueries" ) * multiplier;
136142
limitKnnQueries = Integer.parseInt( cdl.get( "limitKnnQueries" ) );
137143
distanceNorm = cdl.get( "distanceNorm" ).trim();

src/main/java/org/polypheny/simpleclient/scenario/vectorbench/queryBuilder/CreateIntFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private static class CreateIntFeatureQuery extends Query {
6666
public String getSql() {
6767
String sql = "CREATE TABLE knn_intfeature (id INTEGER NOT NULL, feature INTEGER ARRAY(1, " + this.dimension + "), PRIMARY KEY(id))";
6868
if ( this.store != null ) {
69-
sql += "ON STORE \"" + this.store + "\"";
69+
sql += " ON STORE \"" + this.store + "\"";
7070
}
7171
return sql;
7272
}

src/main/java/org/polypheny/simpleclient/scenario/vectorbench/queryBuilder/CreateMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static class CreateMetadataQuery extends Query {
6363
public String getSql() {
6464
String sql = "CREATE TABLE knn_metadata (id integer NOT NULL, textdata VARCHAR(100), PRIMARY KEY (id))";
6565
if ( this.store != null ) {
66-
sql += "ON STORE \"" + this.store + "\"";
66+
sql += " ON STORE \"" + this.store + "\"";
6767
}
6868
return sql;
6969
}

src/main/java/org/polypheny/simpleclient/scenario/vectorbench/queryBuilder/CreateRealFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private static class CreateRealFeatureQuery extends Query {
6666
public String getSql() {
6767
String sql = "CREATE TABLE knn_realfeature (id INTEGER NOT NULL, feature REAL ARRAY(1, " + this.dimension + "), PRIMARY KEY(id))";
6868
if ( this.store != null ) {
69-
sql += "ON STORE \"" + this.store + "\"";
69+
sql += " ON STORE \"" + this.store + "\"";
7070
}
7171
return sql;
7272
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019-4/17/26, 2:54 PM 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.HashMap;
32+
import java.util.Map;
33+
import java.util.Random;
34+
35+
public class MetadataKnnRealCrossJoin extends QueryBuilder {
36+
37+
private static final boolean EXPECT_RESULT = true;
38+
39+
private final int dimension;
40+
private final int limit;
41+
private final String norm;
42+
43+
private final Random random;
44+
45+
46+
public MetadataKnnRealCrossJoin( long randomSeed, int dimension, int limit, String norm ) {
47+
this.dimension = dimension;
48+
this.limit = limit;
49+
this.norm = norm;
50+
51+
this.random = new Random( randomSeed );
52+
}
53+
54+
55+
private Float[] getRandomVector() {
56+
Float[] floats = new Float[this.dimension];
57+
for ( int i = 0; i < this.dimension; i++ ) {
58+
floats[i] = random.nextInt( 100 ) / 100.0f;
59+
}
60+
61+
return floats;
62+
}
63+
64+
65+
@Override
66+
public synchronized Query getNewQuery() {
67+
return new MetadataKnnRealCrossJoin.MetadataKnnRealCrossJoinQuery(
68+
getRandomVector(),
69+
limit,
70+
norm
71+
);
72+
}
73+
74+
75+
private static class MetadataKnnRealCrossJoinQuery extends Query {
76+
77+
private static final String SQL_1 = "SELECT knn_metadata.id, knn_metadata.textdata, closest.dist FROM knn_metadata, ( SELECT t1.id, distance(t1.feature, t2.feature, '";
78+
private static final String SQL_2 = "') AS dist FROM knn_realfeature t1, knn_realfeature t2 WHERE t2.id = 1 ORDER BY dist ASC LIMIT ";
79+
private static final String SQL_3 = ") AS closest WHERE knn_metadata.id = closest.id ORDER BY closest.dist ASC";
80+
private final Float[] target;
81+
private final int limit;
82+
private final String norm;
83+
84+
85+
private MetadataKnnRealCrossJoinQuery( Float[] target, int limit, String norm ) {
86+
super( EXPECT_RESULT );
87+
this.target = target;
88+
this.limit = limit;
89+
this.norm = norm;
90+
}
91+
92+
93+
@Override
94+
public String getSql() {
95+
return SQL_1 + norm + SQL_2 + limit + SQL_3;
96+
}
97+
98+
99+
@Override
100+
public String getParameterizedSqlQuery() {
101+
return null;
102+
//return SQL_1 + "?" + SQL_2 + "'" + norm + "'" + SQL_3 + limit + SQL_4;
103+
}
104+
105+
106+
@Override
107+
public Map<Integer, ImmutablePair<DataTypes, Object>> getParameterValues() {
108+
Map<Integer, ImmutablePair<DataTypes, Object>> map = new HashMap<>();
109+
map.put( 1, new ImmutablePair<>( DataTypes.ARRAY_REAL, target ) );
110+
return map;
111+
}
112+
113+
114+
@Override
115+
public HttpRequest<?> getRest() {
116+
return null;
117+
}
118+
119+
120+
@Override
121+
public String getMongoQl() {
122+
return null;
123+
}
124+
125+
}
126+
127+
}
128+

src/main/java/org/polypheny/simpleclient/scenario/vectorbench/queryBuilder/SimpleKnnIdRealFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private static class SimpleKnnIdRealFeatureQuery extends Query {
8080

8181
private static final String SQL_1 = "SELECT closest.dist FROM ( SELECT id, distance(feature, ";
8282
private static final String SQL_2 = ", ";
83-
private static final String SQL_3 = ") AS dist FROM knn_intfeature ORDER BY dist ASC LIMIT ";
83+
private static final String SQL_3 = ") AS dist FROM knn_realfeature ORDER BY dist ASC LIMIT ";
8484
private static final String SQL_4 = ") AS closest";
8585

8686
private final Float[] target;
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019-4/17/26, 2:44 PM 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.Map;
32+
import java.util.Random;
33+
34+
public class SimpleKnnRealCrossJoin extends QueryBuilder {
35+
36+
private static final boolean EXPECT_RESULT = true;
37+
38+
private final int dimension;
39+
private final int limit;
40+
private final String norm;
41+
42+
private final Random random;
43+
44+
45+
public SimpleKnnRealCrossJoin( long randomSeed, int dimension, int limit, String norm ) {
46+
this.dimension = dimension;
47+
this.limit = limit;
48+
this.norm = norm;
49+
50+
this.random = new Random( randomSeed );
51+
}
52+
53+
54+
private Float[] getRandomVector() {
55+
Float[] floats = new Float[this.dimension];
56+
for ( int i = 0; i < this.dimension; i++ ) {
57+
floats[i] = random.nextInt( 100 ) / 100.0f;
58+
}
59+
60+
return floats;
61+
}
62+
63+
64+
@Override
65+
public synchronized Query getNewQuery() {
66+
return new SimpleKnnRealCrossJoin.SimpleKnnRealCrossJoinQuery(
67+
getRandomVector(),
68+
limit,
69+
norm );
70+
}
71+
72+
73+
private static class SimpleKnnRealCrossJoinQuery extends Query {
74+
75+
private static final String SQL_1 = "SELECT t1.id, distance(t1.feature, t2.feature,";
76+
private static final String SQL_2 = ") as dist FROM knn_realfeature t1, knn_realfeature t2 WHERE t2.id = 1 ORDER BY dist ASC LIMIT ";
77+
78+
private final Float[] target;
79+
private final int limit;
80+
private final String norm;
81+
82+
83+
public SimpleKnnRealCrossJoinQuery( Float[] target, int limit, String norm ) {
84+
super( EXPECT_RESULT );
85+
this.target = target;
86+
this.limit = limit;
87+
this.norm = norm;
88+
}
89+
90+
91+
@Override
92+
public String getSql() {
93+
return SQL_1 + " '" + norm + "' " + SQL_2 + limit;
94+
}
95+
96+
97+
@Override
98+
public String getParameterizedSqlQuery() {
99+
return "";
100+
}
101+
102+
103+
@Override
104+
public Map<Integer, ImmutablePair<DataTypes, Object>> getParameterValues() {
105+
return Map.of();
106+
}
107+
108+
109+
@Override
110+
public HttpRequest<?> getRest() {
111+
return null;
112+
}
113+
114+
115+
@Override
116+
public String getMongoQl() {
117+
return null;
118+
}
119+
120+
}
121+
}

src/main/resources/org/polypheny/simpleclient/scenario/vectorbench/vector.properties

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ useRandomSeeds = true
1212
randomSeedInsert = 46891971806236
1313
randomSeedQuery = 196033374268
1414

15-
dimensionFeatureVectors = 10
15+
dimensionFeatureVectors = 100
1616
batchSizeInserts = 2500
1717
batchSizeQueries = 10
1818

1919
# Numbers of queries
2020
numberOfEntries = 100000
21-
numberOfSimpleKnnIntFeatureQueries = 10
22-
numberOfSimpleKnnRealFeatureQueries = 10
21+
numberOfSimpleKnnIntFeatureQueries = 0
22+
numberOfSimpleKnnRealFeatureQueries = 0
2323
numberOfSimpleMetadataQueries = 10
24-
numberOfSimpleKnnIdIntFeatureQueries = 10
25-
numberOfSimpleKnnIdRealFeatureQueries = 10
26-
numberOfMetadataKnnIntFeatureQueries = 10
27-
numberOfMetadataKnnRealFeatureQueries = 10
24+
numberOfSimpleKnnIdIntFeatureQueries = 0
25+
numberOfSimpleKnnIdRealFeatureQueries = 0
26+
numberOfMetadataKnnIntFeatureQueries = 0
27+
numberOfMetadataKnnRealFeatureQueries = 0
28+
numberOfSimpleKnnRealCrossJoinQueries = 10
29+
numberOfMetadataKnnRealCrossJoinQueries = 10
2830

2931
limitKnnQueries = 10
30-
distanceNorm = L2
32+
distanceNorm = COSINE
3133

0 commit comments

Comments
 (0)