|
@@ -9,6 +9,7 @@ package org.elasticsearch.xpack.esql.expression.function.scalar.string;
|
|
|
|
|
|
import org.apache.lucene.util.BytesRef;
|
|
|
import org.elasticsearch.compute.ann.Evaluator;
|
|
|
+import org.elasticsearch.compute.ann.Fixed;
|
|
|
import org.elasticsearch.compute.operator.EvalOperator;
|
|
|
import org.elasticsearch.xpack.esql.expression.function.scalar.UnaryScalarFunction;
|
|
|
import org.elasticsearch.xpack.esql.planner.Mappable;
|
|
@@ -51,7 +52,7 @@ public final class Trim extends UnaryScalarFunction implements Mappable {
|
|
|
Function<Expression, Supplier<EvalOperator.ExpressionEvaluator>> toEvaluator
|
|
|
) {
|
|
|
Supplier<EvalOperator.ExpressionEvaluator> field = toEvaluator.apply(field());
|
|
|
- return () -> new TrimEvaluator(field.get());
|
|
|
+ return () -> new TrimEvaluator(new BytesRef(), field.get());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -65,9 +66,18 @@ public final class Trim extends UnaryScalarFunction implements Mappable {
|
|
|
}
|
|
|
|
|
|
@Evaluator
|
|
|
- static BytesRef process(BytesRef val) {
|
|
|
- // TODO: optimize
|
|
|
- String str = val.utf8ToString();
|
|
|
- return new BytesRef(str.trim());
|
|
|
+ static BytesRef process(@Fixed BytesRef scratch, BytesRef val) {
|
|
|
+ int offset = val.offset;
|
|
|
+ int length = val.length;
|
|
|
+ while ((offset < length) && ((val.bytes[offset] & 0xff) <= 0x20)) {
|
|
|
+ offset++;
|
|
|
+ }
|
|
|
+ while ((offset < length) && ((val.bytes[length - 1] & 0xff) <= 0x20)) {
|
|
|
+ length--;
|
|
|
+ }
|
|
|
+ scratch.bytes = val.bytes;
|
|
|
+ scratch.offset = offset;
|
|
|
+ scratch.length = length - offset;
|
|
|
+ return scratch;
|
|
|
}
|
|
|
}
|