Skip to content

QL: Remove constant_keyword field type #60524

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

Merged
merged 9 commits into from
Nov 5, 2020
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 @@ -28,7 +28,6 @@
import java.util.Objects;
import java.util.StringJoiner;

import static org.elasticsearch.xpack.ql.type.DataTypes.CONSTANT_KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME;
import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.SCALED_FLOAT;
Expand Down Expand Up @@ -214,7 +213,6 @@ protected Object unwrapMultiValue(Object values) {
protected boolean isFromDocValuesOnly(DataType dataType) {
return dataType == KEYWORD // because of ignore_above.
|| dataType == DATETIME
|| dataType == CONSTANT_KEYWORD // because a non-existent value is considered the constant value itself
|| dataType == SCALED_FLOAT; // because of scaling_factor
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.xpack.ql.QlIllegalArgumentException;
import org.elasticsearch.xpack.ql.type.ConstantKeywordEsField;
import org.elasticsearch.xpack.ql.type.DataType;
import org.elasticsearch.xpack.ql.type.DataTypeRegistry;
import org.elasticsearch.xpack.ql.type.DateEsField;
Expand Down Expand Up @@ -65,7 +64,6 @@
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.elasticsearch.action.ActionListener.wrap;
import static org.elasticsearch.xpack.ql.type.DataTypes.CONSTANT_KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME;
import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.OBJECT;
Expand Down Expand Up @@ -313,13 +311,8 @@ public static IndexResolution mergedMappings(DataTypeRegistry typeRegistry, Stri
StringBuilder errorMessage = new StringBuilder();

boolean hasUnmapped = types.containsKey(UNMAPPED);
// a keyword field and a constant_keyword field with the same name in two different indices are considered "compatible"
// since a common use case of constant_keyword field involves two indices with a field having the same name: one being
// a keyword, the other being a constant_keyword
boolean hasCompatibleKeywords = types.containsKey(KEYWORD.esType()) && types.containsKey(CONSTANT_KEYWORD.esType());
int allowedTypesCount = (hasUnmapped ? 2 : 1) + (hasCompatibleKeywords ? 1 : 0);

if (types.size() > allowedTypesCount) {
if (types.size() > (hasUnmapped ? 2 : 1)) {
// build the error message
// and create a MultiTypeField

Expand Down Expand Up @@ -364,11 +357,6 @@ public static IndexResolution mergedMappings(DataTypeRegistry typeRegistry, Stri
}
}

// if there are both a keyword and a constant_keyword type for this field, only keep the keyword as a common compatible type
if (hasCompatibleKeywords) {
types.remove(CONSTANT_KEYWORD.esType());
}

// everything checks
return null;
});
Expand Down Expand Up @@ -460,9 +448,6 @@ private static EsField createField(DataTypeRegistry typeRegistry, String fieldNa
if (esType == DATETIME) {
return new DateEsField(fieldName, props, isAggregateable);
}
if (esType == CONSTANT_KEYWORD) {
return new ConstantKeywordEsField(fieldName);
}
if (esType == UNSUPPORTED) {
return new UnsupportedEsField(fieldName, typeName, null, props);
}
Expand Down Expand Up @@ -567,7 +552,6 @@ private static List<EsIndex> buildIndices(DataTypeRegistry typeRegistry, String[
}

Map<String, FieldCapabilities> types = new LinkedHashMap<>(entry.getValue());
// apply verification and possibly remove the "duplicate" CONSTANT_KEYWORD field type
final InvalidMappedField invalidField = validityVerifier.apply(fieldName, types);
// apply verification for fields belonging to index aliases
Map<String, InvalidMappedField> invalidFieldsForAliases = getInvalidFieldsForAliases(fieldName, types, aliases);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN;
import static org.elasticsearch.xpack.ql.type.DataTypes.BYTE;
import static org.elasticsearch.xpack.ql.type.DataTypes.CONSTANT_KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME;
import static org.elasticsearch.xpack.ql.type.DataTypes.DOUBLE;
import static org.elasticsearch.xpack.ql.type.DataTypes.FLOAT;
Expand Down Expand Up @@ -124,7 +123,7 @@ public static Converter converterFor(DataType from, DataType to) {
return DefaultConverter.TO_NULL;
}
// proper converters
if (to == KEYWORD || to == TEXT || to == CONSTANT_KEYWORD) {
if (to == KEYWORD || to == TEXT) {
return conversionToString(from);
}
if (to == LONG) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public final class DataTypes {
// string
public static final DataType KEYWORD = new DataType("keyword", Integer.MAX_VALUE, false, false, true);
public static final DataType TEXT = new DataType("text", Integer.MAX_VALUE, false, false, false);
public static final DataType CONSTANT_KEYWORD = new DataType("constant_keyword", Integer.MAX_VALUE, false, false, true);
// date
public static final DataType DATETIME = new DataType("DATETIME", "date", Long.BYTES, false, false, true);
// ip
Expand All @@ -62,7 +61,6 @@ public final class DataTypes {
SCALED_FLOAT,
KEYWORD,
TEXT,
CONSTANT_KEYWORD,
DATETIME,
IP,
BINARY,
Expand Down Expand Up @@ -134,7 +132,7 @@ public static boolean isUnsupported(DataType from) {
}

public static boolean isString(DataType t) {
return t == KEYWORD || t == TEXT || t == CONSTANT_KEYWORD;
return t == KEYWORD || t == TEXT;
}

public static boolean isPrimitive(DataType t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Map;
import java.util.function.Function;

import static org.elasticsearch.xpack.ql.type.DataTypes.CONSTANT_KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.TEXT;

Expand Down Expand Up @@ -45,7 +44,7 @@ public Exact getExactInfo() {
private Tuple<EsField, String> findExact() {
EsField field = null;
for (EsField property : getProperties().values()) {
if ((property.getDataType() == KEYWORD || property.getDataType() == CONSTANT_KEYWORD) && property.getExactInfo().hasExact()) {
if (property.getDataType() == KEYWORD && property.getExactInfo().hasExact()) {
if (field != null) {
return new Tuple<>(null, "Multiple exact keyword candidates available for [" + getName() +
"]; specify which one to use");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Map.Entry;

import static java.util.Collections.emptyMap;
import static org.elasticsearch.xpack.ql.type.DataTypes.CONSTANT_KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME;
import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.NESTED;
Expand Down Expand Up @@ -49,7 +48,7 @@ private static Map<String, EsField> startWalking(DataTypeRegistry typeRegistry,
private static DataType getType(DataTypeRegistry typeRegistry, Map<String, Object> content) {
if (content.containsKey("type")) {
String typeName = content.get("type").toString();
if ("wildcard".equals(typeName)) {
if ("constant_keyword".equals(typeName) || "wildcard".equals(typeName)) {
return KEYWORD;
}
try {
Expand Down Expand Up @@ -94,8 +93,6 @@ private static void walkMapping(DataTypeRegistry typeRegistry, String name, Obje
int length = intSetting(content.get("ignore_above"), Short.MAX_VALUE);
boolean normalized = Strings.hasText(textSetting(content.get("normalizer"), null));
field = new KeywordEsField(name, properties, docValues, length, normalized);
} else if (esDataType == CONSTANT_KEYWORD) {
field = new ConstantKeywordEsField(name);
} else if (esDataType == DATETIME) {
field = new DateEsField(name, properties, docValues);
} else if (esDataType == UNSUPPORTED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static org.elasticsearch.xpack.ql.type.DataTypeConverter.converterFor;
import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN;
import static org.elasticsearch.xpack.ql.type.DataTypes.BYTE;
import static org.elasticsearch.xpack.ql.type.DataTypes.CONSTANT_KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME;
import static org.elasticsearch.xpack.ql.type.DataTypes.DOUBLE;
import static org.elasticsearch.xpack.ql.type.DataTypes.FLOAT;
Expand Down Expand Up @@ -373,7 +372,6 @@ public void testCommonType() {
assertEquals(BOOLEAN, commonType(BOOLEAN, BOOLEAN));
assertEquals(NULL, commonType(NULL, NULL));
assertEquals(INTEGER, commonType(INTEGER, KEYWORD));
assertEquals(DOUBLE, commonType(DOUBLE, CONSTANT_KEYWORD));
assertEquals(LONG, commonType(TEXT, LONG));
assertEquals(SHORT, commonType(SHORT, BYTE));
assertEquals(FLOAT, commonType(BYTE, FLOAT));
Expand All @@ -383,11 +381,6 @@ public void testCommonType() {
// strings
assertEquals(TEXT, commonType(TEXT, KEYWORD));
assertEquals(TEXT, commonType(KEYWORD, TEXT));
assertEquals(TEXT, commonType(TEXT, CONSTANT_KEYWORD));
assertEquals(TEXT, commonType(CONSTANT_KEYWORD, TEXT));
assertEquals(KEYWORD, commonType(KEYWORD, CONSTANT_KEYWORD));
assertEquals(KEYWORD, commonType(CONSTANT_KEYWORD, KEYWORD));
assertEquals(CONSTANT_KEYWORD, commonType(CONSTANT_KEYWORD, CONSTANT_KEYWORD));
}

public void testEsDataTypes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Map;

import static java.util.Collections.emptyMap;
import static org.elasticsearch.xpack.ql.type.DataTypes.CONSTANT_KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME;
import static org.elasticsearch.xpack.ql.type.DataTypes.INTEGER;
import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD;
Expand Down Expand Up @@ -139,7 +138,6 @@ public void testMultiField() {
assertThat(fields.size(), is(4));
assertThat(fields.get("raw").getDataType(), is(KEYWORD));
assertThat(fields.get("english").getDataType(), is(TEXT));
assertThat(fields.get("constant").getDataType(), is(CONSTANT_KEYWORD));
assertThat(fields.get("wildcard").getDataType(), is(KEYWORD));
}

Expand All @@ -154,7 +152,6 @@ public void testMultiFieldTooManyOptions() {
assertThat(fields.size(), is(4));
assertThat(fields.get("raw").getDataType(), is(KEYWORD));
assertThat(fields.get("english").getDataType(), is(TEXT));
assertThat(fields.get("constant").getDataType(), is(CONSTANT_KEYWORD));
assertThat(fields.get("wildcard").getDataType(), is(KEYWORD));
}

Expand Down Expand Up @@ -182,7 +179,7 @@ public void testConstantKeywordField() {
Map<String, EsField> mapping = loadMapping("mapping-constant-keyword.json");
assertThat(mapping.size(), is(1));
EsField dt = mapping.get("full_name");
assertThat(dt.getDataType().typeName(), is("constant_keyword"));
assertThat(dt.getDataType().typeName(), is("keyword"));
}

public void testWildcardField() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public enum EsType implements SQLType {
SCALED_FLOAT( Types.FLOAT),
KEYWORD( Types.VARCHAR),
TEXT( Types.VARCHAR),
CONSTANT_KEYWORD( Types.VARCHAR),
OBJECT( Types.STRUCT),
NESTED( Types.STRUCT),
BINARY( Types.VARBINARY),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ static Object convert(Object v, EsType columnType, String typeString) throws SQL
case BOOLEAN:
case TEXT:
case KEYWORD:
case CONSTANT_KEYWORD:
return v; // These types are already represented correctly in JSON
case BYTE:
return ((Number) v).byteValue(); // Parser might return it as integer or long - need to update to the correct type
Expand Down Expand Up @@ -328,7 +327,6 @@ private static Boolean asBoolean(Object val, EsType columnType, String typeStrin
return Boolean.valueOf(Integer.signum(((Number) val).intValue()) != 0);
case KEYWORD:
case TEXT:
case CONSTANT_KEYWORD:
return Boolean.valueOf((String) val);
default:
return failConversion(val, columnType, typeString, Boolean.class);
Expand All @@ -351,7 +349,6 @@ private static Byte asByte(Object val, EsType columnType, String typeString) thr
return safeToByte(safeToLong(((Number) val).doubleValue()));
case KEYWORD:
case TEXT:
case CONSTANT_KEYWORD:
try {
return Byte.valueOf((String) val);
} catch (NumberFormatException e) {
Expand Down Expand Up @@ -379,7 +376,6 @@ private static Short asShort(Object val, EsType columnType, String typeString) t
return safeToShort(safeToLong(((Number) val).doubleValue()));
case KEYWORD:
case TEXT:
case CONSTANT_KEYWORD:
try {
return Short.valueOf((String) val);
} catch (NumberFormatException e) {
Expand All @@ -406,7 +402,6 @@ private static Integer asInteger(Object val, EsType columnType, String typeStrin
return safeToInt(safeToLong(((Number) val).doubleValue()));
case KEYWORD:
case TEXT:
case CONSTANT_KEYWORD:
try {
return Integer.valueOf((String) val);
} catch (NumberFormatException e) {
Expand Down Expand Up @@ -438,7 +433,6 @@ private static Long asLong(Object val, EsType columnType, String typeString) thr
// return ((Number) val).longValue();
case KEYWORD:
case TEXT:
case CONSTANT_KEYWORD:
try {
return Long.valueOf((String) val);
} catch (NumberFormatException e) {
Expand Down Expand Up @@ -466,7 +460,6 @@ private static Float asFloat(Object val, EsType columnType, String typeString) t
return Float.valueOf(((Number) val).floatValue());
case KEYWORD:
case TEXT:
case CONSTANT_KEYWORD:
try {
return Float.valueOf((String) val);
} catch (NumberFormatException e) {
Expand All @@ -493,7 +486,6 @@ private static Double asDouble(Object val, EsType columnType, String typeString)
return Double.valueOf(((Number) val).doubleValue());
case KEYWORD:
case TEXT:
case CONSTANT_KEYWORD:
try {
return Double.valueOf((String) val);
} catch (NumberFormatException e) {
Expand Down Expand Up @@ -559,7 +551,6 @@ private static BigDecimal asBigDecimal(Object val, EsType columnType, String typ
return BigDecimal.valueOf(((Number) val).doubleValue());
case KEYWORD:
case TEXT:
case CONSTANT_KEYWORD:
try {
return new BigDecimal((String) val);
} catch (NumberFormatException nfe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ private TypeUtils() {}
types.put(EsType.SCALED_FLOAT, Double.class);
types.put(EsType.KEYWORD, String.class);
types.put(EsType.TEXT, String.class);
types.put(EsType.CONSTANT_KEYWORD, String.class);
types.put(EsType.BINARY, byte[].class);
types.put(EsType.DATETIME, Timestamp.class);
types.put(EsType.IP, String.class);
Expand Down Expand Up @@ -158,7 +157,7 @@ static EsType of(String name) throws SQLException {
}

static boolean isString(EsType dataType) {
return dataType == EsType.KEYWORD || dataType == EsType.TEXT || dataType == EsType.CONSTANT_KEYWORD;
return dataType == EsType.KEYWORD || dataType == EsType.TEXT;
}

static EsType of(Class<? extends Object> clazz) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import static org.elasticsearch.xpack.ql.type.DataTypeConverter.DefaultConverter.TO_NULL;
import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN;
import static org.elasticsearch.xpack.ql.type.DataTypes.BYTE;
import static org.elasticsearch.xpack.ql.type.DataTypes.CONSTANT_KEYWORD;
import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME;
import static org.elasticsearch.xpack.ql.type.DataTypes.DOUBLE;
import static org.elasticsearch.xpack.ql.type.DataTypes.FLOAT;
Expand Down Expand Up @@ -160,7 +159,7 @@ public static Converter converterFor(DataType from, DataType to) {
}
// extend the default converter with DATE and TIME
if (from == DATE || from == TIME) {
if (to == KEYWORD || to == TEXT || to == CONSTANT_KEYWORD) {
if (to == KEYWORD || to == TEXT) {
return conversionToString(from);
}
if (to == LONG) {
Expand Down
Loading