forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Add Terms Query Support for Calcite RelNode Conversion #2
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
Open
abhishek00159
wants to merge
3
commits into
vinaykpud:feature/datafusion-dsl
Choose a base branch
from
abhishek00159:feature/datafusion-dsl-term-query
base: feature/datafusion-dsl
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
plugins/query-dsl-calcite/src/main/java/org/opensearch/dsl/query/TermsQueryTranslator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.dsl.query; | ||
|
|
||
| import org.apache.calcite.rel.type.RelDataTypeField; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
| import org.opensearch.dsl.exception.ConversionException; | ||
| import org.opensearch.dsl.ConversionContext; | ||
| import org.opensearch.index.query.AbstractQueryBuilder; | ||
| import org.opensearch.index.query.QueryBuilder; | ||
| import org.opensearch.index.query.TermsQueryBuilder; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Converts a {@link TermsQueryBuilder} to a Calcite IN RexNode. | ||
| */ | ||
| public class TermsQueryTranslator implements QueryTranslator { | ||
|
|
||
| @Override | ||
| public Class<? extends QueryBuilder> getQueryType() { | ||
| return TermsQueryBuilder.class; | ||
| } | ||
|
|
||
| @Override | ||
| public RexNode convert(QueryBuilder query, ConversionContext ctx) throws ConversionException { | ||
| ctx.requireOperatorSupported(SqlStdOperatorTable.IN); | ||
|
|
||
| TermsQueryBuilder termsQuery = (TermsQueryBuilder) query; | ||
|
|
||
| if (termsQuery.boost() != AbstractQueryBuilder.DEFAULT_BOOST) { | ||
| throw new RuntimeException("Terms query does not support non-default boost"); | ||
|
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. We need to throw ConversionException to follow the same convention used in other places |
||
| } | ||
| if (termsQuery.queryName() != null) { | ||
| throw new RuntimeException("Terms query does not support _name"); | ||
| } | ||
| if (termsQuery.valueType() != TermsQueryBuilder.ValueType.DEFAULT) { | ||
| throw new RuntimeException("Terms query does not support non-default value_type"); | ||
| } | ||
|
|
||
| String fieldName = termsQuery.fieldName(); | ||
| List<?> values = termsQuery.values(); | ||
|
|
||
| if (values == null || values.isEmpty()) { | ||
| throw new RuntimeException("Terms query must have values"); | ||
| } | ||
|
|
||
| RelDataTypeField field = ctx.getRowType().getField(fieldName, false, false); | ||
| if (field == null) { | ||
| throw new RuntimeException("Field '" + fieldName + "' not found in schema"); | ||
| } | ||
|
|
||
| RexNode fieldRef = ctx.getRexBuilder().makeInputRef(field.getType(), field.getIndex()); | ||
| List<RexNode> literals = values.stream() | ||
| .map(value -> ctx.getRexBuilder().makeLiteral(value, field.getType(), true)) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return ctx.getRexBuilder().makeIn(fieldRef, literals); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can this be done in the setup method of IT instead of every method?