Skip to content
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

EQL: Add concat function #55193

Merged
merged 11 commits into from
May 5, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,44 @@ query = '''
file where between(file_path, "dev", ".json", true) == "\\TestLogs\\something"
'''

[[queries]]
Copy link
Contributor

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:

  • with one pattern only
  • with an empty string pattern among other patterns
  • with null patterns

Copy link
Contributor

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

description = "test string concatenation. update test to avoid case-sensitivity issues"
query = '''
process where concat(serial_event_id, '::', process_name, '::', opcode) == '5::wininit.exe::3'
'''
expected_event_ids = [5]


[[queries]]
description = "test string concatenation. updated test to avoid case-sensitivity issues"
query = '''
process where concat(serial_event_id, '::', null, '::', 3) == '5::::3'
'''
expected_event_ids = [5]


[[queries]]
description = "test string concatenation with null behavior"
query = '''
process where concat(serial_event_id, '::', parent_process_path, '::', 3) == '5::::3'
'''
expected_event_ids = [5]


[[queries]]
query = 'process where concat(serial_event_id) = "1"'
expected_event_ids = [1]

[[queries]]
query = 'process where serial_event_id < 5 and concat(null) == ""'
expected_event_ids = [1, 2, 3, 4]


[[queries]]
query = 'process where serial_event_id < 5 and concat(null, null, null) == ""'
expected_event_ids = [1, 2, 3, 4]


[[queries]]
query = 'process where string(serial_event_id) = "1"'
expected_event_ids = [1]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.elasticsearch.xpack.eql.expression.function.scalar.string.CIDRMatch;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.Between;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.Concat;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.EndsWith;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.IndexOf;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.Length;
Expand All @@ -34,6 +35,7 @@ private static FunctionDefinition[][] functions() {
new FunctionDefinition[] {
def(Between.class, Between::new, 2, "between"),
def(CIDRMatch.class, CIDRMatch::new, "cidrmatch"),
def(Concat.class, Concat::new, "concat"),
def(EndsWith.class, EndsWith::new, "endswith"),
def(IndexOf.class, IndexOf::new, "indexof"),
def(Length.class, Length::new, "length"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.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;
int index = 0;
for (Expression value : values) {
resolution = isExact(value, sourceText(), ParamOrdinal.fromIndex(index));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only isExact? Shouldn't this be a string only type of value?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this loop goes through all of concat's values and returns the resolution of the last element in list. Shouldn't, maybe, stop at the first resolution that is .unresolved() and return that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 9295e79

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, this accepts non string parameters as well, and calls .toString() on them
the example uses pid which is a long
https://eql.readthedocs.io/en/latest/query-guide/functions.html#concat


if (resolution.unresolved()) {
return resolution;
}

index++;
}

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);
}

}
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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) {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.elasticsearch.xpack.eql.expression.function.scalar.whitelist;

import org.elasticsearch.xpack.eql.expression.function.scalar.string.BetweenFunctionProcessor;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.ConcatFunctionProcessor;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.EndsWithFunctionProcessor;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.IndexOfFunctionProcessor;
import org.elasticsearch.xpack.eql.expression.function.scalar.string.LengthFunctionProcessor;
Expand All @@ -16,6 +17,8 @@
import org.elasticsearch.xpack.eql.expression.function.scalar.string.ToStringFunctionProcessor;
import org.elasticsearch.xpack.ql.expression.function.scalar.whitelist.InternalQlScriptUtils;

import java.util.List;

/*
* Whitelisted class for EQL scripts.
* Acts as a registry of the various static methods used <b>internally</b> by the scalar functions
Expand All @@ -29,6 +32,10 @@ public static String between(String s, String left, String right, Boolean greedy
return (String) BetweenFunctionProcessor.doProcess(s, left, right, greedy, caseSensitive);
}

public static String concat(List<Object> values) {
return (String) ConcatFunctionProcessor.doProcess(values);
}

public static Boolean endsWith(String s, String pattern) {
return (Boolean) EndsWithFunctionProcessor.doProcess(s, pattern);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class org.elasticsearch.xpack.eql.expression.function.scalar.whitelist.InternalE
# ASCII Functions
#
String between(String, String, String, Boolean, Boolean)
String concat(java.util.List)
Boolean endsWith(String, String)
Integer indexOf(String, String, Number)
Integer length(String)
Expand Down
Loading