-
Notifications
You must be signed in to change notification settings - Fork 25.2k
EQL: Add string function #54470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
EQL: Add string function #54470
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0b310e3
EQL: Add string() function
rw-access 17dd8d3
EQL: Reorder queryfolder_tests
rw-access ee2ab21
EQL: Add test queries
rw-access 169dc2a
Merge remote-tracking branch 'origin/master' into eql/string-function
rw-access de21d44
EQL: Fix InternalEqlScriptUtils.string and test case
rw-access 4c66c87
EQL: Fix testStringFunctionWithText error message
rw-access 8b5fac0
EQL: Flatten ToStringFunctionPipe.equals
rw-access a037dda
EQL: Reorder painless whitelist
rw-access 509ffd9
Merge remote-tracking branch 'origin/master' into eql/string-function
rw-access 9ca95a6
Merge branch 'master' into eql/string-function
rw-access 25f9e6b
EQL: Address feedback and remove string(null) handling
rw-access dedf571
EQL: Move string(pid) test over
rw-access 2f08b0c
EQL: Rename source -> value
rw-access bd9ecce
Merge branch 'master' into eql/string-function
rw-access File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
104 changes: 104 additions & 0 deletions
104
...src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToString.java
This file contains hidden or 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,104 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.eql.expression.function.scalar.string; | ||
|
||
import org.elasticsearch.xpack.ql.expression.Expression; | ||
import org.elasticsearch.xpack.ql.expression.Expressions; | ||
import org.elasticsearch.xpack.ql.expression.Expressions.ParamOrdinal; | ||
import org.elasticsearch.xpack.ql.expression.FieldAttribute; | ||
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction; | ||
import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe; | ||
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate; | ||
import org.elasticsearch.xpack.ql.expression.gen.script.Scripts; | ||
import org.elasticsearch.xpack.ql.tree.NodeInfo; | ||
import org.elasticsearch.xpack.ql.tree.Source; | ||
import org.elasticsearch.xpack.ql.type.DataType; | ||
import org.elasticsearch.xpack.ql.type.DataTypes; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import static java.lang.String.format; | ||
import static org.elasticsearch.xpack.eql.expression.function.scalar.string.ToStringFunctionProcessor.doProcess; | ||
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isExact; | ||
import static org.elasticsearch.xpack.ql.expression.gen.script.ParamsBuilder.paramsBuilder; | ||
|
||
/** | ||
* EQL specific string function that wraps object.toString. | ||
*/ | ||
public class ToString extends ScalarFunction { | ||
|
||
private final Expression value; | ||
|
||
public ToString(Source source, Expression src) { | ||
super(source, Collections.singletonList(src)); | ||
this.value = src; | ||
} | ||
|
||
@Override | ||
protected TypeResolution resolveType() { | ||
if (!childrenResolved()) { | ||
return new TypeResolution("Unresolved children"); | ||
} | ||
|
||
return isExact(value, sourceText(), ParamOrdinal.DEFAULT); | ||
} | ||
|
||
@Override | ||
protected Pipe makePipe() { | ||
return new ToStringFunctionPipe(source(), this, Expressions.pipe(value)); | ||
} | ||
|
||
@Override | ||
public boolean foldable() { | ||
return value.foldable(); | ||
} | ||
|
||
@Override | ||
public Object fold() { | ||
return doProcess(value.fold()); | ||
} | ||
|
||
@Override | ||
protected NodeInfo<? extends Expression> info() { | ||
return NodeInfo.create(this, ToString::new, value); | ||
} | ||
|
||
@Override | ||
public ScriptTemplate asScript() { | ||
ScriptTemplate sourceScript = asScript(value); | ||
|
||
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{eql}.%s(%s)"), | ||
"string", | ||
sourceScript.template()), | ||
paramsBuilder() | ||
.script(sourceScript.params()) | ||
.build(), dataType()); | ||
} | ||
|
||
@Override | ||
public ScriptTemplate scriptWithField(FieldAttribute field) { | ||
return new ScriptTemplate(processScript(Scripts.DOC_VALUE), | ||
paramsBuilder().variable(field.exactAttribute().name()).build(), | ||
dataType()); | ||
} | ||
|
||
@Override | ||
public DataType dataType() { | ||
return DataTypes.KEYWORD; | ||
} | ||
|
||
@Override | ||
public Expression replaceChildren(List<Expression> newChildren) { | ||
if (newChildren.size() != 1) { | ||
throw new IllegalArgumentException("expected [1] children but received [" + newChildren.size() + "]"); | ||
} | ||
|
||
return new ToString(source(), newChildren.get(0)); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...a/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToStringFunctionPipe.java
This file contains hidden or 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,90 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.eql.expression.function.scalar.string; | ||
|
||
import org.elasticsearch.xpack.ql.execution.search.QlSourceBuilder; | ||
import org.elasticsearch.xpack.ql.expression.Expression; | ||
import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe; | ||
import org.elasticsearch.xpack.ql.tree.NodeInfo; | ||
import org.elasticsearch.xpack.ql.tree.Source; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class ToStringFunctionPipe extends Pipe { | ||
|
||
private final Pipe source; | ||
|
||
public ToStringFunctionPipe(Source source, Expression expression, Pipe src) { | ||
super(source, expression, Collections.singletonList(src)); | ||
this.source = src; | ||
} | ||
|
||
@Override | ||
public final Pipe replaceChildren(List<Pipe> newChildren) { | ||
if (newChildren.size() != 1) { | ||
throw new IllegalArgumentException("expected [1] children but received [" + newChildren.size() + "]"); | ||
} | ||
return new ToStringFunctionPipe(source(), expression(), newChildren.get(0)); | ||
} | ||
|
||
@Override | ||
public final Pipe resolveAttributes(AttributeResolver resolver) { | ||
Pipe newSource = source.resolveAttributes(resolver); | ||
if (newSource == source) { | ||
return this; | ||
} | ||
return replaceChildren(Collections.singletonList(newSource)); | ||
} | ||
|
||
@Override | ||
public boolean supportedByAggsOnlyQuery() { | ||
return source.supportedByAggsOnlyQuery(); | ||
} | ||
|
||
@Override | ||
public boolean resolved() { | ||
return source.resolved(); | ||
} | ||
|
||
@Override | ||
public final void collectFields(QlSourceBuilder sourceBuilder) { | ||
source.collectFields(sourceBuilder); | ||
} | ||
|
||
@Override | ||
protected NodeInfo<ToStringFunctionPipe> info() { | ||
return NodeInfo.create(this, ToStringFunctionPipe::new, expression(), source); | ||
} | ||
|
||
@Override | ||
public ToStringFunctionProcessor asProcessor() { | ||
return new ToStringFunctionProcessor(source.asProcessor()); | ||
} | ||
|
||
public Pipe src() { | ||
return source; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(source); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
return Objects.equals(source, ((ToStringFunctionPipe) obj).source); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
.../elasticsearch/xpack/eql/expression/function/scalar/string/ToStringFunctionProcessor.java
This file contains hidden or 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,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.eql.expression.function.scalar.string; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.xpack.ql.expression.gen.processor.Processor; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class ToStringFunctionProcessor implements Processor { | ||
|
||
public static final String NAME = "sstr"; | ||
|
||
private final Processor source; | ||
|
||
public ToStringFunctionProcessor(Processor source) { | ||
this.source = source; | ||
} | ||
|
||
public ToStringFunctionProcessor(StreamInput in) throws IOException { | ||
source = in.readNamedWriteable(Processor.class); | ||
} | ||
|
||
@Override | ||
public final void writeTo(StreamOutput out) throws IOException { | ||
out.writeNamedWriteable(source); | ||
} | ||
|
||
@Override | ||
public Object process(Object input) { | ||
return doProcess(source.process(input)); | ||
} | ||
|
||
public static Object doProcess(Object source) { | ||
return source == null ? null : source.toString(); | ||
} | ||
|
||
protected Processor source() { | ||
return source; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
ToStringFunctionProcessor other = (ToStringFunctionProcessor) obj; | ||
return Objects.equals(source(), other.source()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(source()); | ||
} | ||
|
||
|
||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.