-
Notifications
You must be signed in to change notification settings - Fork 25.2k
EQL: Add concat function #55193
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 concat function #55193
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bcc4033
EQL: Add concat function
rw-access bd14685
EQL: for loop spacing for concat
rw-access 9295e79
EQL: return unresolved arguments to concat early
rw-access aadfc05
Merge remote-tracking branch 'origin/master' into eql/concat-function
rw-access 1050a0d
EQL: Add concat integration tests
rw-access 0140300
EQL: Fix concat query fail test
rw-access 53ab872
Merge branch 'master' into eql/concat-function
rw-access f9e0f5a
EQL: Add class for concat function testing
rw-access 6c28ee3
EQL: Add concat integration tests
rw-access fbfbc1b
EQL: Update concat() null behavior
rw-access badd0d1
Merge remote-tracking branch 'origin/master' into eql/concat-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
106 changes: 106 additions & 0 deletions
106
...l/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Concat.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,106 @@ | ||
/* | ||
* 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.function.scalar.ScalarFunction; | ||
import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe; | ||
import org.elasticsearch.xpack.ql.expression.gen.script.ParamsBuilder; | ||
import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate; | ||
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.ArrayList; | ||
import java.util.List; | ||
import java.util.StringJoiner; | ||
|
||
import static org.elasticsearch.xpack.eql.expression.function.scalar.string.ConcatFunctionProcessor.doProcess; | ||
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isExact; | ||
import static org.elasticsearch.xpack.ql.expression.gen.script.ParamsBuilder.paramsBuilder; | ||
|
||
/** | ||
* EQL specific concat function to build a string of all input arguments concatenated. | ||
*/ | ||
public class Concat extends ScalarFunction { | ||
|
||
private final List<Expression> values; | ||
|
||
public Concat(Source source, List<Expression> values) { | ||
super(source, values); | ||
this.values = values; | ||
} | ||
|
||
@Override | ||
protected TypeResolution resolveType() { | ||
if (!childrenResolved()) { | ||
return new TypeResolution("Unresolved children"); | ||
} | ||
|
||
TypeResolution resolution = TypeResolution.TYPE_RESOLVED; | ||
for (Expression value : values) { | ||
resolution = isExact(value, sourceText(), Expressions.ParamOrdinal.DEFAULT); | ||
|
||
if (resolution.unresolved()) { | ||
return resolution; | ||
} | ||
} | ||
|
||
return resolution; | ||
} | ||
|
||
@Override | ||
protected Pipe makePipe() { | ||
return new ConcatFunctionPipe(source(), this, Expressions.pipe(values)); | ||
} | ||
|
||
@Override | ||
public boolean foldable() { | ||
return Expressions.foldable(values); | ||
} | ||
|
||
@Override | ||
public Object fold() { | ||
return doProcess(Expressions.fold(values)); | ||
} | ||
|
||
@Override | ||
protected NodeInfo<? extends Expression> info() { | ||
return NodeInfo.create(this, Concat::new, values); | ||
} | ||
|
||
@Override | ||
public ScriptTemplate asScript() { | ||
List<ScriptTemplate> templates = new ArrayList<>(); | ||
for (Expression ex : children()) { | ||
templates.add(asScript(ex)); | ||
} | ||
|
||
StringJoiner template = new StringJoiner(",", "{eql}.concat([", "])"); | ||
ParamsBuilder params = paramsBuilder(); | ||
|
||
for (ScriptTemplate scriptTemplate : templates) { | ||
template.add(scriptTemplate.template()); | ||
params.script(scriptTemplate.params()); | ||
} | ||
|
||
return new ScriptTemplate(formatTemplate(template.toString()), params.build(), dataType()); | ||
} | ||
|
||
@Override | ||
public DataType dataType() { | ||
return DataTypes.KEYWORD; | ||
} | ||
|
||
@Override | ||
public Expression replaceChildren(List<Expression> newChildren) { | ||
return new Concat(source(), newChildren); | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
...ava/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionPipe.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,105 @@ | ||
/* | ||
* 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.expression.gen.processor.Processor; | ||
import org.elasticsearch.xpack.ql.tree.NodeInfo; | ||
import org.elasticsearch.xpack.ql.tree.Source; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class ConcatFunctionPipe extends Pipe { | ||
|
||
private final List<Pipe> values; | ||
|
||
public ConcatFunctionPipe(Source source, Expression expression, List<Pipe> values) { | ||
super(source, expression, values); | ||
this.values = values; | ||
} | ||
|
||
@Override | ||
public final Pipe replaceChildren(List<Pipe> newChildren) { | ||
return new ConcatFunctionPipe(source(), expression(), newChildren); | ||
} | ||
|
||
@Override | ||
public final Pipe resolveAttributes(AttributeResolver resolver) { | ||
List<Pipe> newValues = new ArrayList<>(values.size()); | ||
for (Pipe v : values) { | ||
newValues.add(v.resolveAttributes(resolver)); | ||
} | ||
|
||
if (newValues == values) { | ||
return this; | ||
} | ||
|
||
return replaceChildren(newValues); | ||
} | ||
|
||
@Override | ||
public boolean supportedByAggsOnlyQuery() { | ||
for (Pipe p : values) { | ||
if (p.supportedByAggsOnlyQuery() == false) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean resolved() { | ||
for (Pipe p : values) { | ||
if (p.resolved() == false) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public final void collectFields(QlSourceBuilder sourceBuilder) { | ||
for (Pipe v : values) { | ||
v.collectFields(sourceBuilder); | ||
} | ||
} | ||
|
||
@Override | ||
protected NodeInfo<ConcatFunctionPipe> info() { | ||
return NodeInfo.create(this, ConcatFunctionPipe::new, expression(), values); | ||
} | ||
|
||
@Override | ||
public ConcatFunctionProcessor asProcessor() { | ||
List<Processor> processors = new ArrayList<>(values.size()); | ||
for (Pipe p: values) { | ||
processors.add(p.asProcessor()); | ||
} | ||
return new ConcatFunctionProcessor(processors); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(values); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
return Objects.equals(values, ((ConcatFunctionPipe) obj).values); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...rg/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionProcessor.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,83 @@ | ||
/* | ||
* 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.StreamOutput; | ||
import org.elasticsearch.xpack.ql.expression.gen.processor.Processor; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class ConcatFunctionProcessor implements Processor { | ||
|
||
public static final String NAME = "scon"; | ||
|
||
private final List<Processor> values; | ||
|
||
public ConcatFunctionProcessor(List<Processor> values) { | ||
this.values = values; | ||
} | ||
|
||
@Override | ||
public final void writeTo(StreamOutput out) throws IOException { | ||
for (Processor v: values) { | ||
out.writeNamedWriteable(v); | ||
} | ||
} | ||
|
||
@Override | ||
public Object process(Object input) { | ||
List<Object> processed = new ArrayList<>(values.size()); | ||
for (Processor v: values) { | ||
processed.add(v.process(input)); | ||
} | ||
return doProcess(processed); | ||
} | ||
|
||
public static Object doProcess(List<Object> inputs) { | ||
if (inputs == null) { | ||
return null; | ||
} | ||
|
||
StringBuilder str = new StringBuilder(); | ||
|
||
for (Object input: inputs) { | ||
if (input == null) { | ||
return null; | ||
} | ||
|
||
str.append(input.toString()); | ||
} | ||
|
||
return str.toString(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
return Objects.equals(values, ((ConcatFunctionProcessor) obj).values); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(values); | ||
} | ||
|
||
|
||
@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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add more integration tests here:
null
patternsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With all nulls as well