Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ ROW message = "foobar"
```


Patterns may be specified with ReST query placeholders as well

```esql
FROM employees
| WHERE first_name LIKE ?pattern
| KEEP first_name, last_name
```

```{applies_to}
stack: ga 9.3
```

Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ ROW message = "foobar"
```


Patterns may be specified with ReST query placeholders as well

```esql
FROM employees
| WHERE first_name RLIKE ?pattern
| KEEP first_name, last_name
```

```{applies_to}
stack: ga 9.3
```

Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,42 @@ public void testMatchFunctionAcrossMultipleIndicesWithMissingField() throws IOEx
}
}

public void testParamsWithLike() throws IOException {
assumeTrue("like parameter support", EsqlCapabilities.Cap.LIKE_PARAMETER_SUPPORT.isEnabled());
bulkLoadTestData(11);
var query = requestObjectBuilder().query(format(null, "from {} | where keyword like ?pattern | keep keyword", testIndexName()))
.params("[{\"pattern\" : \"key*0\"}]");
Map<String, Object> result = runEsql(query);
assertEquals(List.of(List.of("keyword0"), List.of("keyword10")), result.get("values"));
}

public void testParamsWithLikeList() throws IOException {
assumeTrue("like parameter support", EsqlCapabilities.Cap.LIKE_PARAMETER_SUPPORT.isEnabled());
bulkLoadTestData(11);
var query = requestObjectBuilder().query(format(null, "from {} | where keyword like (?p1, ?p2) | keep keyword", testIndexName()))
.params("[{\"p1\" : \"key*0\"}, {\"p2\" : \"key*1\"}]");
Map<String, Object> result = runEsql(query);
assertEquals(List.of(List.of("keyword0"), List.of("keyword1"), List.of("keyword10")), result.get("values"));
}

public void testParamsWithRLike() throws IOException {
assumeTrue("like parameter support", EsqlCapabilities.Cap.LIKE_PARAMETER_SUPPORT.isEnabled());
bulkLoadTestData(11);
var query = requestObjectBuilder().query(format(null, "from {} | where keyword rlike ?pattern | keep keyword", testIndexName()))
.params("[{\"pattern\" : \"key.*0\"}]");
Map<String, Object> result = runEsql(query);
assertEquals(List.of(List.of("keyword0"), List.of("keyword10")), result.get("values"));
}

public void testParamsWithRLikeList() throws IOException {
assumeTrue("like parameter support", EsqlCapabilities.Cap.LIKE_PARAMETER_SUPPORT.isEnabled());
bulkLoadTestData(11);
var query = requestObjectBuilder().query(format(null, "from {} | where keyword rlike (?p1, ?p2) | keep keyword", testIndexName()))
.params("[{\"p1\" : \"key.*0\"}, {\"p2\" : \"key.*1\"}]");
Map<String, Object> result = runEsql(query);
assertEquals(List.of(List.of("keyword0"), List.of("keyword1"), List.of("keyword10")), result.get("values"));
}

protected static Request prepareRequestWithOptions(RequestObjectBuilder requestObject, Mode mode) throws IOException {
requestObject.build();
Request request = prepareRequest(mode);
Expand Down
13 changes: 9 additions & 4 deletions x-pack/plugin/esql/src/main/antlr/parser/Expression.g4
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ booleanExpression
;

regexBooleanExpression
: valueExpression (NOT)? LIKE string #likeExpression
| valueExpression (NOT)? RLIKE string #rlikeExpression
| valueExpression (NOT)? LIKE LP string (COMMA string )* RP #likeListExpression
| valueExpression (NOT)? RLIKE LP string (COMMA string )* RP #rlikeListExpression
: valueExpression (NOT)? LIKE stringOrParameter #likeExpression
| valueExpression (NOT)? RLIKE stringOrParameter #rlikeExpression
| valueExpression (NOT)? LIKE LP stringOrParameter (COMMA stringOrParameter )* RP #likeListExpression
| valueExpression (NOT)? RLIKE LP stringOrParameter (COMMA stringOrParameter )* RP #rlikeListExpression
;

stringOrParameter
: string
| parameter
;

matchBooleanExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,11 @@ public enum Cap {
*/
EXPONENTIAL_HISTOGRAM_PERCENTILES_SUPPORT(EXPONENTIAL_HISTOGRAM_FEATURE_FLAG),

/**
* Support like/rlike parameters https://github.com/elastic/elasticsearch/issues/131356
*/
LIKE_PARAMETER_SUPPORT,

// Last capability should still have a comma for fewer merge conflicts when adding new ones :)
// This comment prevents the semicolon from being on the previous capability when Spotless formats the file.
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,50 @@ public class RLike extends RegexMatch<RLikePattern> {
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "RLike", RLike::new);
public static final String NAME = "RLIKE";

@FunctionInfo(returnType = "boolean", description = """
Use `RLIKE` to filter data based on string patterns using
<<regexp-syntax,regular expressions>>. `RLIKE` usually acts on a field placed on
the left-hand side of the operator, but it can also act on a constant (literal)
expression. The right-hand side of the operator represents the pattern.""", detailedDescription = """
Matching special characters (eg. `.`, `*`, `(`...) will require escaping.
The escape character is backslash `\\`. Since also backslash is a special character in string literals,
it will require further escaping.

<<load-esql-example, file=string tag=rlikeEscapingSingleQuotes>>

To reduce the overhead of escaping, we suggest using triple quotes strings `\"\"\"`

<<load-esql-example, file=string tag=rlikeEscapingTripleQuotes>>
```{applies_to}
stack: ga 9.2
serverless: ga
```

Both a single pattern or a list of patterns are supported. If a list of patterns is provided,
the expression will return true if any of the patterns match.

<<load-esql-example, file=where-like tag=rlikeListDocExample>>
""", operator = NAME, examples = @Example(file = "docs", tag = "rlike"))
@FunctionInfo(
returnType = "boolean",
description = """
Use `RLIKE` to filter data based on string patterns using
<<regexp-syntax,regular expressions>>. `RLIKE` usually acts on a field placed on
the left-hand side of the operator, but it can also act on a constant (literal)
expression. The right-hand side of the operator represents the pattern.""",

// we use an inline example here because ?pattern not supported in csv-spec test
detailedDescription = """
Matching special characters (eg. `.`, `*`, `(`...) will require escaping.
The escape character is backslash `\\`. Since also backslash is a special character in string literals,
it will require further escaping.

<<load-esql-example, file=string tag=rlikeEscapingSingleQuotes>>

To reduce the overhead of escaping, we suggest using triple quotes strings `\"\"\"`

<<load-esql-example, file=string tag=rlikeEscapingTripleQuotes>>
```{applies_to}
stack: ga 9.2
serverless: ga
```

Both a single pattern or a list of patterns are supported. If a list of patterns is provided,
the expression will return true if any of the patterns match.

<<load-esql-example, file=where-like tag=rlikeListDocExample>>

Patterns may be specified with ReST query placeholders as well

```esql
FROM employees
| WHERE first_name RLIKE ?pattern
| KEEP first_name, last_name
```

```{applies_to}
stack: ga 9.3
```
""",
operator = NAME,
examples = @Example(file = "docs", tag = "rlike")
)
public RLike(
Source source,
@Param(name = "str", type = { "keyword", "text" }, description = "A literal value.") Expression value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,55 @@ public class WildcardLike extends RegexMatch<WildcardPattern> {
);
public static final String NAME = "LIKE";

@FunctionInfo(returnType = "boolean", description = """
Use `LIKE` to filter data based on string patterns using wildcards. `LIKE`
usually acts on a field placed on the left-hand side of the operator, but it can
also act on a constant (literal) expression. The right-hand side of the operator
represents the pattern.

The following wildcard characters are supported:

* `*` matches zero or more characters.
* `?` matches one character.""", detailedDescription = """
Matching the exact characters `*` and `.` will require escaping.
The escape character is backslash `\\`. Since also backslash is a special character in string literals,
it will require further escaping.

<<load-esql-example, file=string tag=likeEscapingSingleQuotes>>

To reduce the overhead of escaping, we suggest using triple quotes strings `\"\"\"`

<<load-esql-example, file=string tag=likeEscapingTripleQuotes>>

```{applies_to}
stack: ga 9.1
serverless: ga
```
Both a single pattern or a list of patterns are supported. If a list of patterns is provided,
the expression will return true if any of the patterns match.

<<load-esql-example, file=where-like tag=likeListDocExample>>

""", operator = NAME, examples = @Example(file = "docs", tag = "like"))
@FunctionInfo(
returnType = "boolean",
description = """
Use `LIKE` to filter data based on string patterns using wildcards. `LIKE`
usually acts on a field placed on the left-hand side of the operator, but it can
also act on a constant (literal) expression. The right-hand side of the operator
represents the pattern.

The following wildcard characters are supported:

* `*` matches zero or more characters.
* `?` matches one character.""",

// we use an inline example here because ?pattern not supported in csv-spec test
detailedDescription = """
Matching the exact characters `*` and `.` will require escaping.
The escape character is backslash `\\`. Since also backslash is a special character in string literals,
it will require further escaping.

<<load-esql-example, file=string tag=likeEscapingSingleQuotes>>

To reduce the overhead of escaping, we suggest using triple quotes strings `\"\"\"`

<<load-esql-example, file=string tag=likeEscapingTripleQuotes>>

```{applies_to}
stack: ga 9.1
serverless: ga
```
Both a single pattern or a list of patterns are supported. If a list of patterns is provided,
the expression will return true if any of the patterns match.

<<load-esql-example, file=where-like tag=likeListDocExample>>

Patterns may be specified with ReST query placeholders as well

```esql
FROM employees
| WHERE first_name LIKE ?pattern
| KEEP first_name, last_name
```

```{applies_to}
stack: ga 9.3
```
""",
operator = NAME,
examples = @Example(file = "docs", tag = "like")
)
public WildcardLike(
Source source,
@Param(name = "str", type = { "keyword", "text" }, description = "A literal expression.") Expression left,
Expand Down

Large diffs are not rendered by default.

Loading
Loading