ES|QL: No plain strings in Literal#129399
Conversation
|
Pinging @elastic/es-analytical-engine (Team:Analytics) |
| } else if (dataType == VERSION && value instanceof BytesRef br) { | ||
| str = new Version(br).toString(); | ||
| // } else if (dataType == IP && value instanceof BytesRef ip) { | ||
| // str = DocValueFormat.IP.format(ip); |
There was a problem hiding this comment.
Just in case, is this intentionally commented?
There was a problem hiding this comment.
Yeah... unfortunately it is, for two reasons:
- IP conversion from/to BytesRefs could trigger assertion errors, and it's pretty annoying in tests
- We are explicitly using strings for IPs in some cases, see CobineDisjunctions; we'll have to change that before we can force IPs as BytesRefs
There was a problem hiding this comment.
Could you please add this as a comment/todo referencing an issue to resolve it eventually?
There was a problem hiding this comment.
I created an issue and I'm linking it in a TODO
| 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.
Is this an improvement? Or were Versions a readable String before?
There was a problem hiding this comment.
We had some cases (mostly unit tests) where we passed plain strings to VERSION literals.
Even for tests it's not a good thing, as we are testing something that is different than the actual production execution.
| ; | ||
|
|
||
| cdirMatchEqualsInsOrsIPs | ||
| cicdirMatchEqualsInsOrsIPs |
There was a problem hiding this comment.
| cicdirMatchEqualsInsOrsIPs | |
| cidrMatchEqualsInsOrsIPs |
There was a problem hiding this comment.
Nit: I would replace every cast+utf8ToString() to BytesRefs.toString(), if possible
| } | ||
|
|
||
| private static String stringEvaluator(ToIp.LeadingZeros leadingZeros) { | ||
| if (leadingZeros == null) { |
There was a problem hiding this comment.
As this was here before, and in jdk21 the case null works (right?), do we need this change?
There was a problem hiding this comment.
My IntelliJ (Java 21) keeps complaining, but maybe it's some wrong setting on my side
| if (value instanceof String) { | ||
| return false; | ||
| } | ||
| if (value instanceof Collection<?> c) { | ||
| return c.stream().allMatch(x -> noPlainStrings(x, dataType)); | ||
| } |
There was a problem hiding this comment.
Nit: consider pattern matching here
| if (value instanceof String) { | |
| return false; | |
| } | |
| if (value instanceof Collection<?> c) { | |
| return c.stream().allMatch(x -> noPlainStrings(x, dataType)); | |
| } | |
| return switch (value) { | |
| case String s -> false; | |
| case Collection<?> c -> c.stream().allMatch(x -> noPlainStrings(x, dataType)); | |
| default -> true; | |
| } |
| } | ||
|
|
||
| public static Literal l(Object value, DataType type) { | ||
| if (value instanceof String && (type == DataType.TEXT || type == DataType.KEYWORD)) { |
There was a problem hiding this comment.
NIT: type == DataType.TEXT || type == DataType.KEYWORD theck looks a bit cheaper. Do you mind making it first (before instanceof)?
| return (Literal) value; | ||
| } | ||
| DataType type = DataType.fromJava(value); | ||
| if (value instanceof String && (type == TEXT || type == KEYWORD)) { |
| if (folded instanceof Integer num) { | ||
| checkNumber(num); | ||
| return toEvaluator.apply(new Literal(source(), " ".repeat(num), KEYWORD)); | ||
| return toEvaluator.apply(new Literal(source(), BytesRefs.toBytesRef(" ".repeat(num)), KEYWORD)); |
There was a problem hiding this comment.
This seems to repeat a lot, I wonder if it is worth introducing:
pusblic static Literal.keyword(Source source, String literal) {
return new Literal(source, BytesRefs.toBytesRef(literal), KEYWORD);
}
to simplify construction of string literals?
There was a problem hiding this comment.
👍 I think it makes sense
|
Thanks for the feedback folks! |
💔 Backport failed
You can use sqren/backport to manually backport by running |
|
Manual backport #129702 |
Fixes: #129322
Making sure that all the
TEXT,KEYWORDandVERSIONLiterals contain a BytesRef (and not a plainjava.lang.String).The mix of the two was the reason for some ClassCastExceptions, eg. #129322
Steps: