-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ESQL: LTRIM, RTRIM and fix unicode whitespace (#98590)
Here we add support for the following two ESQL functions: * LTRIM: remove leading spaces from a string * RTRIM: remove trailing spaces from a string We also fix an issue with the handling of unicode white spaces. We make use of unicode code points to identify unicode whitespace characters instead of relying on ASCII codes. Moreover, iterating bytes in a Unicode string needs to consider that some Unicode characters are encoded using multiple bytes.
- Loading branch information
Showing
18 changed files
with
605 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
"Distributed", | ||
"Downsampling", | ||
"EQL", | ||
"ES|QL", | ||
"Engine", | ||
"FIPS", | ||
"Features", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 98590 | ||
summary: "ESQL: LTRIM, RTRIM and fix unicode whitespace" | ||
area: ES|QL | ||
type: feature | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[[esql-ltrim]] | ||
=== `LTRIM` | ||
Removes leading whitespaces from strings. | ||
|
||
[source.merge.styled,esql] | ||
---- | ||
include::{esql-specs}/string.csv-spec[tag=ltrim] | ||
---- | ||
[%header.monospaced.styled,format=dsv,separator=|] | ||
|=== | ||
include::{esql-specs}/string.csv-spec[tag=ltrim-result] | ||
|=== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[[esql-rtrim]] | ||
=== `RTRIM` | ||
Removes trailing whitespaces from strings. | ||
|
||
[source.merge.styled,esql] | ||
---- | ||
include::{esql-specs}/string.csv-spec[tag=rtrim] | ||
---- | ||
[%header.monospaced.styled,format=dsv,separator=|] | ||
|=== | ||
include::{esql-specs}/string.csv-spec[tag=rtrim-result] | ||
|=== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...erated/org/elasticsearch/xpack/esql/expression/function/scalar/string/LTrimEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License | ||
// 2.0; you may not use this file except in compliance with the Elastic License | ||
// 2.0. | ||
package org.elasticsearch.xpack.esql.expression.function.scalar.string; | ||
|
||
import java.lang.Override; | ||
import java.lang.String; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.compute.data.Block; | ||
import org.elasticsearch.compute.data.BytesRefBlock; | ||
import org.elasticsearch.compute.data.BytesRefVector; | ||
import org.elasticsearch.compute.data.Page; | ||
import org.elasticsearch.compute.operator.EvalOperator; | ||
|
||
/** | ||
* {@link EvalOperator.ExpressionEvaluator} implementation for {@link LTrim}. | ||
* This class is generated. Do not edit it. | ||
*/ | ||
public final class LTrimEvaluator implements EvalOperator.ExpressionEvaluator { | ||
private final EvalOperator.ExpressionEvaluator val; | ||
|
||
public LTrimEvaluator(EvalOperator.ExpressionEvaluator val) { | ||
this.val = val; | ||
} | ||
|
||
@Override | ||
public Block eval(Page page) { | ||
Block valUncastBlock = val.eval(page); | ||
if (valUncastBlock.areAllValuesNull()) { | ||
return Block.constantNullBlock(page.getPositionCount()); | ||
} | ||
BytesRefBlock valBlock = (BytesRefBlock) valUncastBlock; | ||
BytesRefVector valVector = valBlock.asVector(); | ||
if (valVector == null) { | ||
return eval(page.getPositionCount(), valBlock); | ||
} | ||
return eval(page.getPositionCount(), valVector).asBlock(); | ||
} | ||
|
||
public BytesRefBlock eval(int positionCount, BytesRefBlock valBlock) { | ||
BytesRefBlock.Builder result = BytesRefBlock.newBlockBuilder(positionCount); | ||
BytesRef valScratch = new BytesRef(); | ||
position: for (int p = 0; p < positionCount; p++) { | ||
if (valBlock.isNull(p) || valBlock.getValueCount(p) != 1) { | ||
result.appendNull(); | ||
continue position; | ||
} | ||
result.appendBytesRef(LTrim.process(valBlock.getBytesRef(valBlock.getFirstValueIndex(p), valScratch))); | ||
} | ||
return result.build(); | ||
} | ||
|
||
public BytesRefVector eval(int positionCount, BytesRefVector valVector) { | ||
BytesRefVector.Builder result = BytesRefVector.newVectorBuilder(positionCount); | ||
BytesRef valScratch = new BytesRef(); | ||
position: for (int p = 0; p < positionCount; p++) { | ||
result.appendBytesRef(LTrim.process(valVector.getBytesRef(p, valScratch))); | ||
} | ||
return result.build(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "LTrimEvaluator[" + "val=" + val + "]"; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...erated/org/elasticsearch/xpack/esql/expression/function/scalar/string/RTrimEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License | ||
// 2.0; you may not use this file except in compliance with the Elastic License | ||
// 2.0. | ||
package org.elasticsearch.xpack.esql.expression.function.scalar.string; | ||
|
||
import java.lang.Override; | ||
import java.lang.String; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.compute.data.Block; | ||
import org.elasticsearch.compute.data.BytesRefBlock; | ||
import org.elasticsearch.compute.data.BytesRefVector; | ||
import org.elasticsearch.compute.data.Page; | ||
import org.elasticsearch.compute.operator.EvalOperator; | ||
|
||
/** | ||
* {@link EvalOperator.ExpressionEvaluator} implementation for {@link RTrim}. | ||
* This class is generated. Do not edit it. | ||
*/ | ||
public final class RTrimEvaluator implements EvalOperator.ExpressionEvaluator { | ||
private final EvalOperator.ExpressionEvaluator val; | ||
|
||
public RTrimEvaluator(EvalOperator.ExpressionEvaluator val) { | ||
this.val = val; | ||
} | ||
|
||
@Override | ||
public Block eval(Page page) { | ||
Block valUncastBlock = val.eval(page); | ||
if (valUncastBlock.areAllValuesNull()) { | ||
return Block.constantNullBlock(page.getPositionCount()); | ||
} | ||
BytesRefBlock valBlock = (BytesRefBlock) valUncastBlock; | ||
BytesRefVector valVector = valBlock.asVector(); | ||
if (valVector == null) { | ||
return eval(page.getPositionCount(), valBlock); | ||
} | ||
return eval(page.getPositionCount(), valVector).asBlock(); | ||
} | ||
|
||
public BytesRefBlock eval(int positionCount, BytesRefBlock valBlock) { | ||
BytesRefBlock.Builder result = BytesRefBlock.newBlockBuilder(positionCount); | ||
BytesRef valScratch = new BytesRef(); | ||
position: for (int p = 0; p < positionCount; p++) { | ||
if (valBlock.isNull(p) || valBlock.getValueCount(p) != 1) { | ||
result.appendNull(); | ||
continue position; | ||
} | ||
result.appendBytesRef(RTrim.process(valBlock.getBytesRef(valBlock.getFirstValueIndex(p), valScratch))); | ||
} | ||
return result.build(); | ||
} | ||
|
||
public BytesRefVector eval(int positionCount, BytesRefVector valVector) { | ||
BytesRefVector.Builder result = BytesRefVector.newVectorBuilder(positionCount); | ||
BytesRef valScratch = new BytesRef(); | ||
position: for (int p = 0; p < positionCount; p++) { | ||
result.appendBytesRef(RTrim.process(valVector.getBytesRef(p, valScratch))); | ||
} | ||
return result.build(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "RTrimEvaluator[" + "val=" + val + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.