-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor parsing logic of the Query Builder (second try) #1824
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.knn; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
import org.opensearch.cluster.ClusterModule; | ||
import org.opensearch.common.xcontent.LoggingDeprecationHandler; | ||
import org.opensearch.common.xcontent.XContentFactory; | ||
import org.opensearch.common.xcontent.json.JsonXContent; | ||
import org.opensearch.core.common.bytes.BytesArray; | ||
import org.opensearch.core.common.bytes.BytesReference; | ||
import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.index.query.QueryBuilder; | ||
import org.opensearch.index.query.QueryBuilders; | ||
import org.opensearch.index.query.TermQueryBuilder; | ||
import org.opensearch.knn.index.query.KNNQueryBuilder; | ||
import org.opensearch.knn.index.query.parser.KNNQueryBuilderParser; | ||
import org.opensearch.plugins.SearchPlugin; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Benchmarks for impact of changes around query parsing | ||
*/ | ||
@Warmup(iterations = 5, time = 10) | ||
@Measurement(iterations = 3, time = 10) | ||
@Fork(3) | ||
@State(Scope.Benchmark) | ||
public class QueryParsingBenchmarks { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really want to have this file in knn repo? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be kept. I think its fine to have this for testing in the future if we want to make some other change around this functionality.
Sure, this is something we could consider in future, but out of scope for this PR. Want to try to get this change in without having to change multiple other things. |
||
private static final TermQueryBuilder TERM_QUERY = QueryBuilders.termQuery("field", "value"); | ||
private static final NamedXContentRegistry NAMED_X_CONTENT_REGISTRY = xContentRegistry(); | ||
|
||
@Param({ "128", "1024" }) | ||
private int dimension; | ||
@Param({ "basic", "filter" }) | ||
private String type; | ||
|
||
private BytesReference bytesReference; | ||
|
||
@Setup | ||
public void setup() throws IOException { | ||
XContentBuilder builder = XContentFactory.jsonBuilder(); | ||
builder.startObject(); | ||
builder.startObject("test"); | ||
builder.field(KNNQueryBuilder.VECTOR_FIELD.getPreferredName(), generateVectorWithOnes(dimension)); | ||
builder.field(KNNQueryBuilder.K_FIELD.getPreferredName(), 1); | ||
if (type.equals("filter")) { | ||
builder.field(KNNQueryBuilder.FILTER_FIELD.getPreferredName(), TERM_QUERY); | ||
} | ||
builder.endObject(); | ||
builder.endObject(); | ||
bytesReference = BytesReference.bytes(builder); | ||
} | ||
|
||
@Benchmark | ||
public void fromXContent(final Blackhole bh) throws IOException { | ||
XContentParser xContentParser = createParser(); | ||
bh.consume(KNNQueryBuilderParser.fromXContent(xContentParser)); | ||
} | ||
|
||
private XContentParser createParser() throws IOException { | ||
XContentParser contentParser = createParser(bytesReference); | ||
contentParser.nextToken(); | ||
return contentParser; | ||
} | ||
|
||
private float[] generateVectorWithOnes(final int dimensions) { | ||
float[] vector = new float[dimensions]; | ||
Arrays.fill(vector, (float) 1); | ||
return vector; | ||
} | ||
|
||
private XContentParser createParser(final BytesReference data) throws IOException { | ||
BytesArray array = (BytesArray) data; | ||
return JsonXContent.jsonXContent.createParser( | ||
NAMED_X_CONTENT_REGISTRY, | ||
LoggingDeprecationHandler.INSTANCE, | ||
array.array(), | ||
array.offset(), | ||
array.length() | ||
); | ||
} | ||
|
||
private static NamedXContentRegistry xContentRegistry() { | ||
List<NamedXContentRegistry.Entry> list = ClusterModule.getNamedXWriteables(); | ||
SearchPlugin.QuerySpec<?> spec = new SearchPlugin.QuerySpec<>( | ||
TermQueryBuilder.NAME, | ||
TermQueryBuilder::new, | ||
TermQueryBuilder::fromXContent | ||
); | ||
list.add(new NamedXContentRegistry.Entry(QueryBuilder.class, spec.getName(), (p, c) -> spec.getParser().fromXContent(p))); | ||
return new NamedXContentRegistry(list); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are using this copyright
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will update. I think this is broken: https://github.com/opensearch-project/k-NN/blob/main/.idea/copyright/SPDX_ALv2.xml.