Skip to content

Commit

Permalink
ESQL: Extra tests for trim/ltrim/rtrim
Browse files Browse the repository at this point in the history
This adds some extra tests, docs, and descriptions for the `trim`,
`ltrim`, and `rtrim` functions.
  • Loading branch information
nik9000 committed Nov 16, 2023
1 parent f89f82a commit ca0ee3f
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 47 deletions.
7 changes: 7 additions & 0 deletions docs/reference/esql/functions/ltrim.asciidoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[discrete]
[[esql-ltrim]]
=== `LTRIM`
[.text-center]
image::esql/functions/signature/ltrim.svg[Embedded,opts=inline]

Removes leading whitespaces from strings.

[source.merge.styled,esql]
Expand All @@ -11,3 +14,7 @@ include::{esql-specs}/string.csv-spec[tag=ltrim]
|===
include::{esql-specs}/string.csv-spec[tag=ltrim-result]
|===

Supported types:

include::types/rtrim.asciidoc[]
7 changes: 7 additions & 0 deletions docs/reference/esql/functions/rtrim.asciidoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[discrete]
[[esql-rtrim]]
=== `RTRIM`
[.text-center]
image::esql/functions/signature/rtrim.svg[Embedded,opts=inline]

Removes trailing whitespaces from strings.

[source.merge.styled,esql]
Expand All @@ -11,3 +14,7 @@ include::{esql-specs}/string.csv-spec[tag=rtrim]
|===
include::{esql-specs}/string.csv-spec[tag=rtrim-result]
|===

Supported types:

include::types/rtrim.asciidoc[]
1 change: 0 additions & 1 deletion docs/reference/esql/functions/signature/case.svg

This file was deleted.

2 changes: 1 addition & 1 deletion docs/reference/esql/functions/signature/ltrim.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/reference/esql/functions/signature/rtrim.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/reference/esql/functions/signature/trim.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/reference/esql/functions/types/ltrim.asciidoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[%header.monospaced.styled,format=dsv,separator=|]
|===
arg1 | result
str | result
keyword | keyword
text | text
|===
2 changes: 1 addition & 1 deletion docs/reference/esql/functions/types/rtrim.asciidoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[%header.monospaced.styled,format=dsv,separator=|]
|===
arg1 | result
str | result
keyword | keyword
text | text
|===
2 changes: 1 addition & 1 deletion docs/reference/esql/functions/types/trim.asciidoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[%header.monospaced.styled,format=dsv,separator=|]
|===
arg1 | result
str | result
keyword | keyword
text | text
|===
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ least |"? least(first:integer|long|double|boolean|keyword|tex
left |"? left(string:keyword, length:integer)" |[string, length] |["keyword", "integer"] |["", ""] |? | "" | [false, false] | false
length |? length(arg1:?) |arg1 |? | "" |? | "" | false | false
log10 |"? log10(n:integer|long|double|unsigned_long)" |n |"integer|long|double|unsigned_long" | "" |? | "" | false | false
ltrim |? ltrim(arg1:?) |arg1 |? | "" |? | "" | false | false
ltrim |"keyword|text ltrim(str:keyword|text)" |str |"keyword|text" | "" |"keyword|text" |Removes leading whitespaces from a string.| false | false
max |? max(arg1:?) |arg1 |? | "" |? | "" | false | false
median |? median(arg1:?) |arg1 |? | "" |? | "" | false | false
median_absolute_deviation|? median_absolute_deviation(arg1:?) |arg1 |? | "" |? | "" | false | false
Expand All @@ -60,7 +60,7 @@ pow |"? pow(base:integer|long|double, exponent:integer|doub
replace |"? replace(arg1:?, arg2:?, arg3:?)" | [arg1, arg2, arg3] | [?, ?, ?] |["", "", ""] |? | "" | [false, false, false]| false
right |"? right(string:keyword, length:integer)" |[string, length] |["keyword", "integer"] |["", ""] |? | "" | [false, false] | false
round |? round(arg1:?, arg2:?) |[arg1, arg2] |[?, ?] |["", ""] |? | "" | [false, false] | false
rtrim |? rtrim(arg1:?) |arg1 |? | "" |? | "" | false | false
rtrim |"keyword|text rtrim(str:keyword|text)" |str |"keyword|text" | "" |"keyword|text" |Removes trailing whitespaces from a string.| false | false
sin |"double sin(n:integer|long|double|unsigned_long)" |n |"integer|long|double|unsigned_long" |An angle, in radians |double |Returns the trigonometric sine of an angle | false | false
sinh |"double sinh(n:integer|long|double|unsigned_long)"|n |"integer|long|double|unsigned_long" | "" |double | "" | false | false
split |? split(arg1:?, arg2:?) |[arg1, arg2] |[?, ?] |["", ""] |? | "" | [false, false] | false
Expand Down Expand Up @@ -88,9 +88,9 @@ to_string |"? to_string(v:unsigned_long|date|boolean|double|ip|te
to_ul |? to_ul(arg1:?) |arg1 |? | "" |? | "" | false | false
to_ulong |? to_ulong(arg1:?) |arg1 |? | "" |? | "" | false | false
to_unsigned_long |? to_unsigned_long(arg1:?) |arg1 |? | "" |? | "" | false | false
to_ver |"? to_ver(v:keyword|text|version)" |v |"keyword|text|version"| "" |? | "" | false | false
to_version |"? to_version(v:keyword|text|version)" |v |"keyword|text|version"| "" |? | "" | false | false
trim |? trim(arg1:?) |arg1 |? | "" |? | "" | false | false
to_ver |"? to_ver(v:keyword|text|version)" |v |"keyword|text|version"| "" |? | "" | false | false
to_version |"? to_version(v:keyword|text|version)" |v |"keyword|text|version"| "" |? | "" | false | false
trim |"keyword|text trim(str:keyword|text)" |str |"keyword|text" | "" |"keyword|text" |Removes leading and trailing whitespaces from a string.| false | false
;


Expand Down Expand Up @@ -129,7 +129,7 @@ synopsis:keyword
"? left(string:keyword, length:integer)"
? length(arg1:?)
"? log10(n:integer|long|double|unsigned_long)"
? ltrim(arg1:?)
"keyword|text ltrim(str:keyword|text)"
? max(arg1:?)
? median(arg1:?)
? median_absolute_deviation(arg1:?)
Expand All @@ -149,7 +149,7 @@ synopsis:keyword
"? replace(arg1:?, arg2:?, arg3:?)"
"? right(string:keyword, length:integer)"
? round(arg1:?, arg2:?)
? rtrim(arg1:?)
"keyword|text rtrim(str:keyword|text)"
"double sin(n:integer|long|double|unsigned_long)"
"double sinh(n:integer|long|double|unsigned_long)"
? split(arg1:?, arg2:?)
Expand Down Expand Up @@ -179,7 +179,7 @@ synopsis:keyword
? to_unsigned_long(arg1:?)
"? to_ver(v:keyword|text|version)"
"? to_version(v:keyword|text|version)"
? trim(arg1:?)
"keyword|text trim(str:keyword|text)"
;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.elasticsearch.compute.ann.Evaluator;
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.TypeResolutions;
Expand All @@ -27,8 +29,9 @@
* Removes leading whitespaces from a string.
*/
public class LTrim extends UnaryScalarFunction implements EvaluatorMapper {
public LTrim(Source source, Expression field) {
super(source, field);
@FunctionInfo(returnType = { "keyword", "text" }, description = "Removes leading whitespaces from a string.")
public LTrim(Source source, @Param(name = "str", type = { "keyword", "text" }) Expression str) {
super(source, str);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.elasticsearch.compute.ann.Evaluator;
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.TypeResolutions;
Expand All @@ -27,8 +29,9 @@
* Removes trailing whitespaces from a string.
*/
public class RTrim extends UnaryScalarFunction implements EvaluatorMapper {
public RTrim(Source source, Expression field) {
super(source, field);
@FunctionInfo(returnType = { "keyword", "text" }, description = "Removes trailing whitespaces from a string.")
public RTrim(Source source, @Param(name = "str", type = { "keyword", "text" }) Expression str) {
super(source, str);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.elasticsearch.compute.ann.Evaluator;
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.TypeResolutions;
Expand All @@ -27,8 +29,8 @@
* Removes leading and trailing whitespaces from a string.
*/
public final class Trim extends UnaryScalarFunction implements EvaluatorMapper {

public Trim(Source source, Expression str) {
@FunctionInfo(returnType = { "keyword", "text" }, description = "Removes leading and trailing whitespaces from a string.")
public Trim(Source source, @Param(name = "str", type = { "keyword", "text" }) Expression str) {
super(source, str);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ protected final boolean enableWarningsCheck() {
}

public boolean logResults() {
return false;
return true;
}

private void doTest() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,4 +935,11 @@ static Version randomVersion() {
default -> throw new IllegalArgumentException();
};
}

/**
* All string types (keyword, text, match_only_text, etc).
*/
protected static DataType[] strings() {
return EsqlDataTypes.types().stream().filter(DataTypes::isString).toArray(DataType[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ public Set<DataType> sortedTypesSet(DataType[] validTypes, DataType... additiona
return mergedSet;
}

/**
* All string types (keyword, text, match_only_text, etc). For passing to {@link #required} or {@link #optional}.
*/
protected static DataType[] strings() {
return EsqlDataTypes.types().stream().filter(DataTypes::isString).toArray(DataType[]::new);
}

/**
* All integer types (long, int, short, byte). For passing to {@link #required} or {@link #optional}.
*/
Expand Down
Loading

0 comments on commit ca0ee3f

Please sign in to comment.