|
| 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 SimpleKnnRealFeatureFiltered 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 Random random; |
| 42 | + private final String filterCategory; |
| 43 | + |
| 44 | + public SimpleKnnRealFeatureFiltered( 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 | + private Float[] getRandomVector() { |
| 53 | + Float[] floats = new Float[this.dimension]; |
| 54 | + for ( int i = 0; i < this.dimension; i++ ) { |
| 55 | + floats[i] = random.nextInt( 100 ) / 100.0f; |
| 56 | + } |
| 57 | + return floats; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public synchronized Query getNewQuery() { |
| 62 | + return new SimpleKnnRealFeatureFilteredQuery( getRandomVector(), limit, norm, filterCategory ); |
| 63 | + } |
| 64 | + |
| 65 | + private static class SimpleKnnRealFeatureFilteredQuery extends Query { |
| 66 | + |
| 67 | + private final Float[] target; |
| 68 | + private final int limit; |
| 69 | + private final String norm; |
| 70 | + private final String category; |
| 71 | + |
| 72 | + public SimpleKnnRealFeatureFilteredQuery( Float[] target, int limit, String norm, String category ) { |
| 73 | + super( EXPECT_RESULT ); |
| 74 | + this.target = target; |
| 75 | + this.limit = limit; |
| 76 | + this.norm = norm; |
| 77 | + this.category = category; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String getSql() { |
| 82 | + return "SELECT id, distance(feature, ARRAY" + Arrays.toString( |
| 83 | + target ) + ", '" + norm + "') as dist " + |
| 84 | + "FROM knn_realfeature " + |
| 85 | + "WHERE category = '" + category + "' " + |
| 86 | + "ORDER BY dist ASC LIMIT " + limit; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String getParameterizedSqlQuery() { return null; } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Map<Integer, ImmutablePair<DataTypes, Object>> getParameterValues() { |
| 94 | + return Map.of(); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + @Override |
| 99 | + public HttpRequest<?> getRest() { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + // Note: This query will currently not enable pushdown, therefore runtime is expected to be high. |
| 105 | + @Override |
| 106 | + public String getMongoQl() { |
| 107 | + return "db.knn_realfeature.aggregate([{" + |
| 108 | + " \"$vectorSearch\": {" + |
| 109 | + " \"path\": \"feature\"," + |
| 110 | + " \"queryVector\": " + Arrays.toString( target ) |
| 111 | + + "," + |
| 112 | + " \"metric\": \"" + norm + "\"," + |
| 113 | + " \"limit\": " + limit + "," + |
| 114 | + " \"filter\": { \"category\": \"" + category + |
| 115 | + "\" }" + |
| 116 | + " }" + |
| 117 | + "}])"; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments