Skip to content
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

[7.x] Centralize doc values checking (#77089) #77124

Merged
merged 1 commit into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,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 @@ -215,13 +215,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 @@ -357,13 +357,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 @@ -377,17 +377,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 @@ -1048,15 +1048,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 @@ -54,9 +54,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