-
Notifications
You must be signed in to change notification settings - Fork 25.6k
ES|QL: No plain strings in Literal #129399
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
Changes from 12 commits
af84776
e6f4383
48ea6e0
a7b2928
a3ccc10
79b3550
81194d1
b726351
c7cd50a
6e9475e
e56aab3
094c98f
c301a3b
87796c4
da3f737
67ff8e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,18 +11,24 @@ | |
| import org.elasticsearch.common.io.stream.NamedWriteableRegistry; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.lucene.BytesRefs; | ||
| import org.elasticsearch.xpack.esql.core.QlIllegalArgumentException; | ||
| import org.elasticsearch.xpack.esql.core.tree.NodeInfo; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
| import org.elasticsearch.xpack.esql.core.type.DataType; | ||
| import org.elasticsearch.xpack.esql.core.util.PlanStreamInput; | ||
| import org.elasticsearch.xpack.versionfield.Version; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.elasticsearch.xpack.esql.core.type.DataType.CARTESIAN_POINT; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.GEO_POINT; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.TEXT; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.VERSION; | ||
| import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.CARTESIAN; | ||
| import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.GEO; | ||
|
|
||
|
|
@@ -45,10 +51,23 @@ public class Literal extends LeafExpression { | |
|
|
||
| public Literal(Source source, Object value, DataType dataType) { | ||
| super(source); | ||
| assert noPlainStrings(value, dataType); | ||
| this.dataType = dataType; | ||
| this.value = value; | ||
| } | ||
|
|
||
| private boolean noPlainStrings(Object value, DataType dataType) { | ||
| if (dataType == KEYWORD || dataType == TEXT || dataType == VERSION) { | ||
| if (value instanceof String) { | ||
| return false; | ||
| } | ||
| if (value instanceof Collection<?> c) { | ||
| return c.stream().allMatch(x -> noPlainStrings(x, dataType)); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static Literal readFrom(StreamInput in) throws IOException { | ||
| Source source = Source.readFrom((StreamInput & PlanStreamInput) in); | ||
| Object value = in.readGenericValue(); | ||
|
|
@@ -122,7 +141,20 @@ public boolean equals(Object obj) { | |
|
|
||
| @Override | ||
| public String toString() { | ||
| String str = String.valueOf(value); | ||
| String str; | ||
| if (dataType == KEYWORD || dataType == TEXT) { | ||
| str = BytesRefs.toString(value); | ||
| } else if (dataType == VERSION && value instanceof BytesRef br) { | ||
| str = new Version(br).toString(); | ||
|
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. Is this an improvement? Or were Versions a readable String before? 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. We had some cases (mostly unit tests) where we passed plain strings to VERSION literals. |
||
| // } else if (dataType == IP && value instanceof BytesRef ip) { | ||
| // str = DocValueFormat.IP.format(ip); | ||
|
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. Just in case, is this intentionally commented? 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. Yeah... unfortunately it is, for two reasons:
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. Could you please add this as a comment/todo referencing an issue to resolve it eventually? 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. I created an issue and I'm linking it in a TODO |
||
| } else { | ||
| str = String.valueOf(value); | ||
| } | ||
|
|
||
| if (str == null) { | ||
| str = "null"; | ||
| } | ||
| if (str.length() > 500) { | ||
| return str.substring(0, 500) + "..."; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| package org.elasticsearch.xpack.esql.core.expression.function.scalar; | ||
|
|
||
| import org.elasticsearch.common.lucene.BytesRefs; | ||
| import org.elasticsearch.xpack.esql.core.expression.Literal; | ||
| import org.elasticsearch.xpack.esql.core.type.DataType; | ||
|
|
||
|
|
@@ -15,10 +16,13 @@ | |
| public final class FunctionTestUtils { | ||
|
|
||
| public static Literal l(Object value) { | ||
| return new Literal(EMPTY, value, DataType.fromJava(value)); | ||
| return l(value, DataType.fromJava(value)); | ||
| } | ||
|
|
||
| public static Literal l(Object value, DataType type) { | ||
| if (value instanceof String && (type == DataType.TEXT || type == DataType.KEYWORD)) { | ||
|
||
| value = BytesRefs.toBytesRef(value); | ||
| } | ||
| return new Literal(EMPTY, value, type); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| package org.elasticsearch.xpack.esql.core.util; | ||
|
|
||
| import org.elasticsearch.common.lucene.BytesRefs; | ||
| import org.elasticsearch.xpack.esql.core.expression.FieldAttribute; | ||
| import org.elasticsearch.xpack.esql.core.expression.Literal; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
|
|
@@ -22,6 +23,8 @@ | |
| import static org.elasticsearch.test.ESTestCase.randomFrom; | ||
| import static org.elasticsearch.xpack.esql.core.tree.Source.EMPTY; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.INTEGER; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.TEXT; | ||
|
|
||
| public final class TestUtils { | ||
| private TestUtils() {} | ||
|
|
@@ -39,6 +42,10 @@ public static Literal of(Source source, Object value) { | |
| if (value instanceof Literal) { | ||
| return (Literal) value; | ||
| } | ||
| DataType type = DataType.fromJava(value); | ||
| if (value instanceof String && (type == TEXT || type == KEYWORD)) { | ||
|
||
| value = BytesRefs.toBytesRef(value); | ||
| } | ||
| return new Literal(source, value, DataType.fromJava(value)); | ||
| } | ||
|
|
||
|
|
@@ -58,12 +65,16 @@ public static FieldAttribute getFieldAttribute(String name, DataType dataType) { | |
| return new FieldAttribute(EMPTY, name, new EsField(name + "f", dataType, emptyMap(), true)); | ||
| } | ||
|
|
||
| /** Similar to {@link String#strip()}, but removes the WS throughout the entire string. */ | ||
| /** | ||
| * Similar to {@link String#strip()}, but removes the WS throughout the entire string. | ||
| */ | ||
| public static String stripThrough(String input) { | ||
| return WS_PATTERN.matcher(input).replaceAll(StringUtils.EMPTY); | ||
| } | ||
|
|
||
| /** Returns the input string, but with parts of it having the letter casing changed. */ | ||
| /** | ||
| * Returns the input string, but with parts of it having the letter casing changed. | ||
| */ | ||
| public static String randomCasing(String input) { | ||
| StringBuilder sb = new StringBuilder(input.length()); | ||
| for (int i = 0, inputLen = input.length(), step = (int) Math.sqrt(inputLen); i < inputLen; i += step) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -398,7 +398,7 @@ eth0 |gamma |fe80::cae2:65ff:fece:feb9 | |||||
| lo0 |gamma |fe80::cae2:65ff:fece:feb9 |fe81::cae2:65ff:fece:feb9 | ||||||
| ; | ||||||
|
|
||||||
| cdirMatchEqualsInsOrsIPs | ||||||
| cicdirMatchEqualsInsOrsIPs | ||||||
|
||||||
| cicdirMatchEqualsInsOrsIPs | |
| cidrMatchEqualsInsOrsIPs |
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.
Nit: consider pattern matching here