-
Notifications
You must be signed in to change notification settings - Fork 2k
Make use of Java 17 pattern matching switch statements #17909
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
JaySabva
wants to merge
6
commits into
opensearch-project:main
Choose a base branch
from
JaySabva:main
base: main
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
6 commits
Select commit
Hold shift + click to select a range
a4f4544
ISSUE #17874 Make use of Java 17 pattern matching switch statements
JaySabva baab507
ISSUE #17874 Make use of Java 17 pattern matching switch statements
JaySabva 5d26ea2
ISSUE #17874 Make use of Java 17 pattern matching switch statements
JaySabva 7e221cb
ISSUE #17874 Make use of Java 17 pattern matching switch statements
JaySabva 1b021b6
ISSUE #17874 Make use of Java 17 pattern matching switch statements
JaySabva 5f4340f
ISSUE #17874 Make use of Java 17 pattern matching switch statements
JaySabva 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
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 |
---|---|---|
|
@@ -63,52 +63,39 @@ public NestedHelper(MapperService mapperService) { | |
|
||
/** Returns true if the given query might match nested documents. */ | ||
public boolean mightMatchNestedDocs(Query query) { | ||
if (query instanceof ConstantScoreQuery) { | ||
return mightMatchNestedDocs(((ConstantScoreQuery) query).getQuery()); | ||
} else if (query instanceof BoostQuery) { | ||
return mightMatchNestedDocs(((BoostQuery) query).getQuery()); | ||
} else if (query instanceof MatchAllDocsQuery) { | ||
return true; | ||
} else if (query instanceof MatchNoDocsQuery) { | ||
return false; | ||
} else if (query instanceof TermQuery) { | ||
// We only handle term(s) queries and range queries, which should already | ||
// cover a high majority of use-cases | ||
return mightMatchNestedDocs(((TermQuery) query).getTerm().field()); | ||
} else if (query instanceof TermInSetQuery) { | ||
final TermInSetQuery termInSetQuery = (TermInSetQuery) query; | ||
if (termInSetQuery.getTermsCount() > 0) { | ||
return mightMatchNestedDocs(termInSetQuery.getField()); | ||
} else { | ||
return false; | ||
} | ||
} else if (query instanceof PointRangeQuery) { | ||
return mightMatchNestedDocs(((PointRangeQuery) query).getField()); | ||
} else if (query instanceof IndexOrDocValuesQuery) { | ||
return mightMatchNestedDocs(((IndexOrDocValuesQuery) query).getIndexQuery()); | ||
} else if (query instanceof ApproximateScoreQuery) { | ||
return mightMatchNestedDocs(((ApproximateScoreQuery) query).getOriginalQuery()); | ||
} else if (query instanceof BooleanQuery) { | ||
final BooleanQuery bq = (BooleanQuery) query; | ||
final boolean hasRequiredClauses = bq.clauses().stream().anyMatch(BooleanClause::isRequired); | ||
if (hasRequiredClauses) { | ||
return bq.clauses() | ||
.stream() | ||
.filter(BooleanClause::isRequired) | ||
.map(BooleanClause::query) | ||
.allMatch(this::mightMatchNestedDocs); | ||
} else { | ||
return bq.clauses() | ||
.stream() | ||
.filter(c -> c.occur() == Occur.SHOULD) | ||
.map(BooleanClause::query) | ||
.anyMatch(this::mightMatchNestedDocs); | ||
return switch (query) { | ||
case ConstantScoreQuery csq -> mightMatchNestedDocs(csq.getQuery()); | ||
case BoostQuery bq -> mightMatchNestedDocs(bq.getQuery()); | ||
case MatchAllDocsQuery ignored -> true; | ||
case MatchNoDocsQuery ignored -> false; | ||
case TermQuery tq -> | ||
// We only handle term(s) queries and range queries, which should already | ||
// cover a high majority of use-cases | ||
mightMatchNestedDocs(tq.getTerm().field()); | ||
case TermInSetQuery tisq -> tisq.getTermsCount() > 0 && mightMatchNestedDocs(tisq.getField()); | ||
case PointRangeQuery prq -> mightMatchNestedDocs(prq.getField()); | ||
case IndexOrDocValuesQuery iodvq -> mightMatchNestedDocs(iodvq.getIndexQuery()); | ||
case ApproximateScoreQuery asq -> mightMatchNestedDocs(asq.getOriginalQuery()); | ||
case BooleanQuery bq -> { | ||
boolean hasRequiredClauses = bq.clauses().stream().anyMatch(BooleanClause::isRequired); | ||
if (hasRequiredClauses) { | ||
yield bq.clauses() | ||
.stream() | ||
.filter(BooleanClause::isRequired) | ||
.map(BooleanClause::query) | ||
.allMatch(this::mightMatchNestedDocs); | ||
} else { | ||
yield bq.clauses() | ||
.stream() | ||
.filter(c -> c.occur() == Occur.SHOULD) | ||
.map(BooleanClause::query) | ||
.anyMatch(this::mightMatchNestedDocs); | ||
} | ||
} | ||
} else if (query instanceof OpenSearchToParentBlockJoinQuery) { | ||
return ((OpenSearchToParentBlockJoinQuery) query).getPath() != null; | ||
} else { | ||
return true; | ||
} | ||
case OpenSearchToParentBlockJoinQuery ostpbjq -> ostpbjq.getPath() != null; | ||
case null -> true; | ||
default -> true; | ||
}; | ||
} | ||
|
||
/** Returns true if a query on the given field might match nested documents. */ | ||
|
@@ -137,48 +124,35 @@ boolean mightMatchNestedDocs(String field) { | |
/** Returns true if the given query might match parent documents or documents | ||
* that are nested under a different path. */ | ||
public boolean mightMatchNonNestedDocs(Query query, String nestedPath) { | ||
if (query instanceof ConstantScoreQuery) { | ||
return mightMatchNonNestedDocs(((ConstantScoreQuery) query).getQuery(), nestedPath); | ||
} else if (query instanceof BoostQuery) { | ||
return mightMatchNonNestedDocs(((BoostQuery) query).getQuery(), nestedPath); | ||
} else if (query instanceof MatchAllDocsQuery) { | ||
return true; | ||
} else if (query instanceof MatchNoDocsQuery) { | ||
return false; | ||
} else if (query instanceof TermQuery) { | ||
return mightMatchNonNestedDocs(((TermQuery) query).getTerm().field(), nestedPath); | ||
} else if (query instanceof TermInSetQuery) { | ||
final TermInSetQuery termInSetQuery = (TermInSetQuery) query; | ||
if (termInSetQuery.getTermsCount() > 0) { | ||
return mightMatchNonNestedDocs(termInSetQuery.getField(), nestedPath); | ||
} else { | ||
return false; | ||
} | ||
} else if (query instanceof PointRangeQuery) { | ||
return mightMatchNonNestedDocs(((PointRangeQuery) query).getField(), nestedPath); | ||
} else if (query instanceof IndexOrDocValuesQuery) { | ||
return mightMatchNonNestedDocs(((IndexOrDocValuesQuery) query).getIndexQuery(), nestedPath); | ||
} else if (query instanceof ApproximateScoreQuery) { | ||
return mightMatchNonNestedDocs(((ApproximateScoreQuery) query).getOriginalQuery(), nestedPath); | ||
} else if (query instanceof BooleanQuery) { | ||
final BooleanQuery bq = (BooleanQuery) query; | ||
final boolean hasRequiredClauses = bq.clauses().stream().anyMatch(BooleanClause::isRequired); | ||
if (hasRequiredClauses) { | ||
return bq.clauses() | ||
.stream() | ||
.filter(BooleanClause::isRequired) | ||
.map(BooleanClause::query) | ||
.allMatch(q -> mightMatchNonNestedDocs(q, nestedPath)); | ||
} else { | ||
return bq.clauses() | ||
.stream() | ||
.filter(c -> c.occur() == Occur.SHOULD) | ||
.map(BooleanClause::query) | ||
.anyMatch(q -> mightMatchNonNestedDocs(q, nestedPath)); | ||
return switch (query) { | ||
case ConstantScoreQuery csq -> mightMatchNonNestedDocs(csq.getQuery(), nestedPath); | ||
case BoostQuery bq -> mightMatchNonNestedDocs(bq.getQuery(), nestedPath); | ||
case MatchAllDocsQuery ignored -> true; | ||
case MatchNoDocsQuery ignored -> false; | ||
case TermQuery tq -> mightMatchNonNestedDocs(tq.getTerm().field(), nestedPath); | ||
case TermInSetQuery tisq -> tisq.getTermsCount() > 0 && mightMatchNonNestedDocs(tisq.getField(), nestedPath); | ||
case PointRangeQuery prq -> mightMatchNonNestedDocs(prq.getField(), nestedPath); | ||
case IndexOrDocValuesQuery iodvq -> mightMatchNonNestedDocs(iodvq.getIndexQuery(), nestedPath); | ||
case ApproximateScoreQuery asq -> mightMatchNonNestedDocs(asq.getOriginalQuery(), nestedPath); | ||
case BooleanQuery bq -> { | ||
boolean hasRequiredClauses = bq.clauses().stream().anyMatch(BooleanClause::isRequired); | ||
if (hasRequiredClauses) { | ||
yield bq.clauses() | ||
.stream() | ||
.filter(BooleanClause::isRequired) | ||
.map(BooleanClause::query) | ||
.allMatch(q -> mightMatchNonNestedDocs(q, nestedPath)); | ||
} else { | ||
yield bq.clauses() | ||
.stream() | ||
.filter(c -> c.occur() == Occur.SHOULD) | ||
.map(BooleanClause::query) | ||
.anyMatch(q -> mightMatchNonNestedDocs(q, nestedPath)); | ||
} | ||
} | ||
} else { | ||
return true; | ||
} | ||
case null -> true; | ||
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. Here too |
||
default -> true; | ||
}; | ||
} | ||
|
||
/** Returns true if a query on the given field might match parent documents | ||
|
Oops, something went wrong.
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.
Wouldn't this map to default case?
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.
I think that will raise null pointer exception I tried that in my scratch file.