Skip to content

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
wants to merge 6 commits into
base: main
Choose a base branch
from
43 changes: 17 additions & 26 deletions libs/core/src/main/java/org/opensearch/ExceptionsHelper.java
Original file line number Diff line number Diff line change
@@ -93,35 +93,26 @@ public static OpenSearchException convertToOpenSearchException(Exception e) {
}

public static RestStatus status(Throwable t) {
if (t != null) {
if (t instanceof OpenSearchException) {
return ((OpenSearchException) t).status();
} else if (t instanceof IllegalArgumentException) {
return RestStatus.BAD_REQUEST;
} else if (t instanceof JsonParseException) {
return RestStatus.BAD_REQUEST;
} else if (t instanceof OpenSearchRejectedExecutionException) {
return RestStatus.TOO_MANY_REQUESTS;
} else if (t instanceof NotXContentException) {
return RestStatus.BAD_REQUEST;
}
}
return RestStatus.INTERNAL_SERVER_ERROR;
return switch (t) {
case OpenSearchException ose -> ose.status();
case IllegalArgumentException iae -> RestStatus.BAD_REQUEST;
case JsonParseException jpe -> RestStatus.BAD_REQUEST;
case OpenSearchRejectedExecutionException osre -> RestStatus.TOO_MANY_REQUESTS;
case NotXContentException nxce -> RestStatus.BAD_REQUEST;
case null -> RestStatus.INTERNAL_SERVER_ERROR;
default -> RestStatus.INTERNAL_SERVER_ERROR;
};
}

public static String summaryMessage(Throwable t) {
if (t != null) {
if (t instanceof OpenSearchException) {
return getExceptionSimpleClassName(t) + "[" + t.getMessage() + "]";
} else if (t instanceof IllegalArgumentException) {
return "Invalid argument";
} else if (t instanceof JsonParseException) {
return "Failed to parse JSON";
} else if (t instanceof OpenSearchRejectedExecutionException) {
return "Too many requests";
}
}
return "Internal failure";
return switch (t) {
case OpenSearchException ose -> getExceptionSimpleClassName(t) + "[" + t.getMessage() + "]";
case IllegalArgumentException iae -> "Invalid argument";
case JsonParseException jpe -> "Failed to parse JSON";
case OpenSearchRejectedExecutionException osre -> "Too many requests";
case null -> "Internal failure";
default -> "Internal failure";
};
}

public static Throwable unwrapCause(Throwable t) {
Original file line number Diff line number Diff line change
@@ -166,28 +166,20 @@ private static void deeplyAppendParameter(StringBuilder sbuf, Object o, Set<Obje
}
if (!o.getClass().isArray()) {
safeObjectAppend(sbuf, o);
} else {
// check for primitive array types because they
// unfortunately cannot be cast to Object[]
if (o instanceof boolean[]) {
booleanArrayAppend(sbuf, (boolean[]) o);
} else if (o instanceof byte[]) {
byteArrayAppend(sbuf, (byte[]) o);
} else if (o instanceof char[]) {
charArrayAppend(sbuf, (char[]) o);
} else if (o instanceof short[]) {
shortArrayAppend(sbuf, (short[]) o);
} else if (o instanceof int[]) {
intArrayAppend(sbuf, (int[]) o);
} else if (o instanceof long[]) {
longArrayAppend(sbuf, (long[]) o);
} else if (o instanceof float[]) {
floatArrayAppend(sbuf, (float[]) o);
} else if (o instanceof double[]) {
doubleArrayAppend(sbuf, (double[]) o);
} else {
objectArrayAppend(sbuf, (Object[]) o, seen);
}
return;
}
// check for primitive array types because they
// unfortunately cannot be cast to Object[]
switch (o) {
case boolean[] boolArr -> booleanArrayAppend(sbuf, boolArr);
case byte[] byteArr -> byteArrayAppend(sbuf, byteArr);
case char[] charArr -> charArrayAppend(sbuf, charArr);
case short[] shortArr -> shortArrayAppend(sbuf, shortArr);
case int[] intArr -> intArrayAppend(sbuf, intArr);
case long[] longArr -> longArrayAppend(sbuf, longArr);
case float[] floatArr -> floatArrayAppend(sbuf, floatArr);
case double[] doubleArr -> doubleArrayAppend(sbuf, doubleArr);
default -> objectArrayAppend(sbuf, (Object[]) o, seen);
}
}

Original file line number Diff line number Diff line change
@@ -206,20 +206,15 @@ public Number numberValue() throws IOException {
@Override
public NumberType numberType() throws IOException {
Number number = numberValue();
if (number instanceof Integer) {
return NumberType.INT;
} else if (number instanceof BigInteger) {
return NumberType.BIG_INTEGER;
} else if (number instanceof Long) {
return NumberType.LONG;
} else if (number instanceof Float) {
return NumberType.FLOAT;
} else if (number instanceof Double) {
return NumberType.DOUBLE;
} else if (number instanceof BigDecimal) {
return NumberType.BIG_DECIMAL;
}
throw new IllegalStateException("No matching token for number_type [" + number.getClass() + "]");
return switch (number) {
case Integer ignored -> NumberType.INT;
case BigInteger ignored -> NumberType.BIG_INTEGER;
case Long ignored -> NumberType.LONG;
case Float ignored -> NumberType.FLOAT;
case Double ignored -> NumberType.DOUBLE;
case BigDecimal ignored -> NumberType.BIG_DECIMAL;
default -> throw new IllegalStateException("No matching token for number_type [" + number.getClass() + "]");
};
}

@Override
146 changes: 60 additions & 86 deletions server/src/main/java/org/opensearch/index/search/NestedHelper.java
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;
Copy link
Member

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?

Copy link
Author

@JaySabva JaySabva Apr 14, 2025

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.

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;
Copy link
Member

Choose a reason for hiding this comment

The 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
Loading