Skip to content
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
8 changes: 8 additions & 0 deletions docs/changelog/115245.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pr: 115245
summary: "ESQL: Fix `REVERSE` with backspace character"
area: ES|QL
type: bug
issues:
- 114372
- 115227
- 115228
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.compute.ann.Evaluator;
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.xpack.esql.core.expression.Expression;
Expand Down Expand Up @@ -79,8 +78,6 @@ protected TypeResolution resolveType() {

/**
* Reverses a unicode string, keeping grapheme clusters together
* @param str
* @return
*/
public static String reverseStringWithUnicodeCharacters(String str) {
BreakIterator boundary = BreakIterator.getCharacterInstance(Locale.ROOT);
Expand All @@ -100,10 +97,12 @@ public static String reverseStringWithUnicodeCharacters(String str) {
return reversed.toString();
}

private static boolean isOneByteUTF8(BytesRef ref) {
private static boolean reverseBytesIsReverseUnicode(BytesRef ref) {
int end = ref.offset + ref.length;
for (int i = ref.offset; i < end; i++) {
if (ref.bytes[i] < 0) {
if (ref.bytes[i] < 0 // Anything encoded in multibyte utf-8
|| ref.bytes[i] == 0x28 // Backspace
) {
return false;
}
}
Expand All @@ -112,13 +111,13 @@ private static boolean isOneByteUTF8(BytesRef ref) {

@Evaluator
static BytesRef process(BytesRef val) {
if (isOneByteUTF8(val)) {
if (reverseBytesIsReverseUnicode(val)) {
// this is the fast path. we know we can just reverse the bytes.
BytesRef reversed = BytesRef.deepCopyOf(val);
reverseArray(reversed.bytes, reversed.offset, reversed.length);
return reversed;
}
return BytesRefs.toBytesRef(reverseStringWithUnicodeCharacters(val.utf8ToString()));
return new BytesRef(reverseStringWithUnicodeCharacters(val.utf8ToString()));
}

@Override
Expand Down