-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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 Substring function with Python semantics #53688
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d23ce79
EQL: Add Substring function with Python semantics
costin 8499f3c
Merge branch 'master' into eql/functions-nointeg
costin 537d870
Address feedback
costin 7db46a6
Fix incorrect assignment
costin ffd50c8
Address feedback
costin bd49db7
Fix super initialization
costin 85eec10
Another rename
costin b7c8ba9
Fix failing test
costin 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 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 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 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 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
51 changes: 51 additions & 0 deletions
51
.../main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringUtils.java
This file contains 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,51 @@ | ||
/* | ||
* 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.Strings; | ||
|
||
import static org.elasticsearch.common.Strings.hasLength; | ||
|
||
final class StringUtils { | ||
|
||
private StringUtils() {} | ||
|
||
/** | ||
* Returns a substring using the Python slice semantics, meaning | ||
* start and end can be negative | ||
*/ | ||
static String substringSlice(String string, int start, int end) { | ||
if (hasLength(string) == false) { | ||
return string; | ||
} | ||
|
||
int length = string.length(); | ||
|
||
// handle first negative values | ||
if (start < 0) { | ||
start += length; | ||
} | ||
if (start < 0) { | ||
start = 0; | ||
} | ||
if (end < 0) { | ||
end += length; | ||
} | ||
if (end < 0) { | ||
end = 0; | ||
} | ||
if (end > length) { | ||
end = length; | ||
} | ||
|
||
if (start >= end) { | ||
rw-access marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return org.elasticsearch.xpack.ql.util.StringUtils.EMPTY; | ||
} | ||
|
||
return Strings.substring(string, start, end); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
...rc/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Substring.java
This file contains 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,129 @@ | ||
/* | ||
* 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.Literal; | ||
import org.elasticsearch.xpack.ql.expression.function.OptionalArgument; | ||
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.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.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import static java.lang.String.format; | ||
import static org.elasticsearch.xpack.eql.expression.function.scalar.string.SubstringFunctionProcessor.doProcess; | ||
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isInteger; | ||
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isStringAndExact; | ||
import static org.elasticsearch.xpack.ql.expression.gen.script.ParamsBuilder.paramsBuilder; | ||
|
||
/** | ||
* EQL specific substring function - similar to the one in Python. | ||
* Note this is different than the one in SQL. | ||
*/ | ||
public class Substring extends ScalarFunction implements OptionalArgument { | ||
|
||
private final Expression source, start, end; | ||
|
||
public Substring(Source source, Expression src, Expression start, Expression end) { | ||
super(source, end != null ? Arrays.asList(src, start, end) : Arrays.asList(src, new Literal(source, 0, DataTypes.INTEGER), start)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is no |
||
this.source = src; | ||
this.start = start; | ||
this.end = end != null ? end : new Literal(source, null, DataTypes.NULL); | ||
} | ||
|
||
@Override | ||
protected TypeResolution resolveType() { | ||
if (!childrenResolved()) { | ||
return new TypeResolution("Unresolved children"); | ||
} | ||
|
||
TypeResolution sourceResolution = isStringAndExact(source, sourceText(), ParamOrdinal.FIRST); | ||
if (sourceResolution.unresolved()) { | ||
return sourceResolution; | ||
} | ||
|
||
TypeResolution startResolution = isInteger(start, sourceText(), ParamOrdinal.SECOND); | ||
if (startResolution.unresolved()) { | ||
return startResolution; | ||
} | ||
|
||
return isInteger(end, sourceText(), ParamOrdinal.THIRD); | ||
} | ||
|
||
@Override | ||
protected Pipe makePipe() { | ||
return new SubstringFunctionPipe(source(), this, Expressions.pipe(source), Expressions.pipe(start), Expressions.pipe(end)); | ||
} | ||
|
||
@Override | ||
public boolean foldable() { | ||
return source.foldable() && start.foldable() && end.foldable(); | ||
} | ||
|
||
@Override | ||
public Object fold() { | ||
return doProcess(source.fold(), start.fold(), end.fold()); | ||
} | ||
|
||
@Override | ||
protected NodeInfo<? extends Expression> info() { | ||
return NodeInfo.create(this, Substring::new, source, start, end); | ||
} | ||
|
||
@Override | ||
public ScriptTemplate asScript() { | ||
ScriptTemplate sourceScript = asScript(source); | ||
ScriptTemplate startScript = asScript(start); | ||
ScriptTemplate endScript = asScript(end); | ||
|
||
return asScriptFrom(sourceScript, startScript, endScript); | ||
} | ||
|
||
protected ScriptTemplate asScriptFrom(ScriptTemplate sourceScript, ScriptTemplate startScript, ScriptTemplate endScript) { | ||
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{eql}.%s(%s,%s,%s)"), | ||
"substring", | ||
sourceScript.template(), | ||
startScript.template(), | ||
endScript.template()), | ||
paramsBuilder() | ||
.script(sourceScript.params()) | ||
.script(startScript.params()) | ||
.script(endScript.params()) | ||
.build(), dataType()); | ||
} | ||
|
||
@Override | ||
public ScriptTemplate scriptWithField(FieldAttribute field) { | ||
return new ScriptTemplate(processScript("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() != 3) { | ||
throw new IllegalArgumentException("expected [3] children but received [" + newChildren.size() + "]"); | ||
} | ||
|
||
return new Substring(source(), newChildren.get(0), newChildren.get(1), newChildren.get(2)); | ||
} | ||
} |
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.
This check couldn't be included inside the
?
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.
No because once checks the lower limit, whether
end
is still negative (after potentially addinglength
) while the other checks the upper limit.