Skip to content

Commit

Permalink
Centralize doc values checking (#77089)
Browse files Browse the repository at this point in the history
This adds two utility methods for to validate the parameters to the
`docValueFormat` method and replaces a pile of copy and pasted code with
calls to them. They just emit a standard error message if the any
unsupported parameters are provided.
  • Loading branch information
nik9000 committed Sep 1, 2021
1 parent 0e1efa6 commit 429beba
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,11 @@ public Object valueForDisplay(Object value) {

@Override
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
if (timeZone != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName()
+ "] does not support custom time zones");
}
checkNoTimeZone(timeZone);
if (format == null) {
return DocValueFormat.RAW;
} else {
return new DocValueFormat.Decimal(format);
}
return new DocValueFormat.Decimal(format);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,8 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S

@Override
public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom formats");
}
if (timeZone != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName()
+ "] does not support custom time zones");
}
checkNoFormat(format);
checkNoTimeZone(timeZone);
return DocValueFormat.BOOLEAN;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,8 @@ public Object valueForDisplay(Object value) {

@Override
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom formats");
}
if (timeZone != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones");
}
checkNoFormat(format);
checkNoTimeZone(timeZone);
return DocValueFormat.BOOLEAN;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ public Object valueForDisplay(Object value) {

@Override
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
if (timeZone != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones");
}
checkNoTimeZone(timeZone);
if (format == null) {
return DocValueFormat.RAW;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,8 @@ public Object valueForDisplay(Object value) {

@Override
public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom formats");
}
if (timeZone != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName()
+ "] does not support custom time zones");
}
checkNoFormat(format);
checkNoTimeZone(timeZone);
return DocValueFormat.IP;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.elasticsearch.core.Tuple;
import org.elasticsearch.index.fielddata.IpScriptFieldData;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.script.IpFieldScript;
import org.elasticsearch.script.CompositeFieldScript;
import org.elasticsearch.script.IpFieldScript;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.lookup.SearchLookup;
Expand All @@ -36,7 +36,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -88,14 +87,8 @@ public Object valueForDisplay(Object value) {

@Override
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
if (format != null) {
String message = "Runtime field [%s] of type [%s] does not support custom formats";
throw new IllegalArgumentException(String.format(Locale.ROOT, message, name(), typeName()));
}
if (timeZone != null) {
String message = "Runtime field [%s] of type [%s] does not support custom time zones";
throw new IllegalArgumentException(String.format(Locale.ROOT, message, name(), typeName()));
}
checkNoFormat(format);
checkNoTimeZone(timeZone);
return DocValueFormat.IP;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ public Object valueForDisplay(Object value) {

@Override
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
if (timeZone != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones");
}
checkNoTimeZone(timeZone);
if (format == null) {
return DocValueFormat.RAW;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,32 @@ public boolean eagerGlobalOrdinals() {
return false;
}

/** Return a {@link DocValueFormat} that can be used to display and parse
* values as returned by the fielddata API.
* The default implementation returns a {@link DocValueFormat#RAW}. */
/**
* Pick a {@link DocValueFormat} that can be used to display and parse
* values of fields of this type.
*/
public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) {
checkNoFormat(format);
checkNoTimeZone(timeZone);
return DocValueFormat.RAW;
}

/**
* Validate the provided {@code format} is null.
*/
protected void checkNoFormat(@Nullable String format) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom formats");
}
}

/**
* Validate the provided {@code timeZone} is null.
*/
protected void checkNoTimeZone(@Nullable ZoneId timeZone) {
if (timeZone != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones");
}
return DocValueFormat.RAW;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1060,15 +1060,11 @@ protected Object parseSourceValue(Object value) {

@Override
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
if (timeZone != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName()
+ "] does not support custom time zones");
}
checkNoTimeZone(timeZone);
if (format == null) {
return DocValueFormat.RAW;
} else {
return new DocValueFormat.Decimal(format);
}
return new DocValueFormat.Decimal(format);
}

public Number parsePoint(byte[] value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public class IpScriptFieldTypeTests extends AbstractScriptFieldTypeTestCase {
public void testFormat() throws IOException {
assertThat(simpleMappedFieldType().docValueFormat(null, null), sameInstance(DocValueFormat.IP));
Exception e = expectThrows(IllegalArgumentException.class, () -> simpleMappedFieldType().docValueFormat("ASDFA", null));
assertThat(e.getMessage(), equalTo("Runtime field [test] of type [ip] does not support custom formats"));
assertThat(e.getMessage(), equalTo("Field [test] of type [ip] does not support custom formats"));
e = expectThrows(IllegalArgumentException.class, () -> simpleMappedFieldType().docValueFormat(null, ZoneId.of("America/New_York")));
assertThat(e.getMessage(), equalTo("Runtime field [test] of type [ip] does not support custom time zones"));
assertThat(e.getMessage(), equalTo("Field [test] of type [ip] does not support custom time zones"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,7 @@ public Object valueForDisplay(Object value) {

@Override
public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
if (timeZone != null) {
throw new IllegalArgumentException(
"Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones"
);
}
checkNoTimeZone(timeZone);
return DocValueFormat.UNSIGNED_LONG_SHIFTED;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,8 @@ public Object valueForDisplay(Object value) {

@Override
public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) {
if (format != null) {
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support custom formats");
}
if (timeZone != null) {
throw new IllegalArgumentException(
"Field [" + name() + "] of type [" + typeName() + "] does not support custom time zones"
);
}
checkNoFormat(format);
checkNoTimeZone(timeZone);
return VERSION_DOCVALUE;
}

Expand Down

0 comments on commit 429beba

Please sign in to comment.