Skip to content

Commit

Permalink
EQL: code cleanup and further tests (#58458)
Browse files Browse the repository at this point in the history
Add FunctionPipe tests to all functions. Cleanup functions code.
  • Loading branch information
astefan authored Jun 24, 2020
1 parent 063e98d commit 0f83d57
Show file tree
Hide file tree
Showing 65 changed files with 1,974 additions and 688 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
*/
package org.elasticsearch.xpack.eql.expression.function.scalar.math;

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.Arrays;
import java.util.List;
import java.util.Objects;

public class ToNumberFunctionPipe extends Pipe {

Expand All @@ -21,15 +23,44 @@ public ToNumberFunctionPipe(Source source, Expression expression, Pipe value, Pi
super(source, expression, Arrays.asList(value, base));
this.value = value;
this.base = base;

}

@Override
public final Pipe replaceChildren(List<Pipe> newChildren) {
if (newChildren.size() != 2) {
throw new IllegalArgumentException("expected [2] children but received [" + newChildren.size() + "]");
}
return new ToNumberFunctionPipe(source(), expression(), newChildren.get(0), newChildren.get(1));
return replaceChildren(newChildren.get(0), newChildren.get(1));
}

@Override
public final Pipe resolveAttributes(AttributeResolver resolver) {
Pipe newValue = value.resolveAttributes(resolver);
Pipe newBase = base.resolveAttributes(resolver);
if (newValue == value && newBase == base) {
return this;
}
return replaceChildren(newValue, newBase);
}

@Override
public boolean supportedByAggsOnlyQuery() {
return value.supportedByAggsOnlyQuery() && base.supportedByAggsOnlyQuery();
}

@Override
public boolean resolved() {
return value.resolved() && base.resolved();
}

protected ToNumberFunctionPipe replaceChildren(Pipe newValue, Pipe newBase) {
return new ToNumberFunctionPipe(source(), expression(), newValue, newBase);
}

@Override
public final void collectFields(QlSourceBuilder sourceBuilder) {
value.collectFields(sourceBuilder);
base.collectFields(sourceBuilder);
}

@Override
Expand All @@ -41,4 +72,32 @@ protected NodeInfo<ToNumberFunctionPipe> info() {
public ToNumberFunctionProcessor asProcessor() {
return new ToNumberFunctionProcessor(value.asProcessor(), base.asProcessor());
}


public Pipe value() {
return value;
}

public Pipe base() {
return base;
}

@Override
public int hashCode() {
return Objects.hash(value(), base());
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
return false;
}

ToNumberFunctionPipe other = (ToNumberFunctionPipe) obj;
return Objects.equals(value(), other.value()) && Objects.equals(base(), other.base());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
*/
public class Between extends ScalarFunction implements OptionalArgument {

private final Expression source, left, right, greedy, caseSensitive;
private final Expression input, left, right, greedy, caseSensitive;

public Between(Source source, Expression src, Expression left, Expression right, Expression greedy, Expression caseSensitive) {
super(source, Arrays.asList(src, left, right, toDefault(greedy), toDefault(caseSensitive)));
this.source = src;
public Between(Source source, Expression input, Expression left, Expression right, Expression greedy, Expression caseSensitive) {
super(source, Arrays.asList(input, left, right, toDefault(greedy), toDefault(caseSensitive)));
this.input = input;
this.left = left;
this.right = right;
this.greedy = arguments().get(3);
Expand All @@ -58,7 +58,7 @@ protected TypeResolution resolveType() {
return new TypeResolution("Unresolved children");
}

TypeResolution resolution = isStringAndExact(source, sourceText(), Expressions.ParamOrdinal.FIRST);
TypeResolution resolution = isStringAndExact(input, sourceText(), Expressions.ParamOrdinal.FIRST);
if (resolution.unresolved()) {
return resolution;
}
Expand All @@ -83,48 +83,48 @@ protected TypeResolution resolveType() {

@Override
protected Pipe makePipe() {
return new BetweenFunctionPipe(source(), this, Expressions.pipe(source),
return new BetweenFunctionPipe(source(), this, Expressions.pipe(input),
Expressions.pipe(left), Expressions.pipe(right),
Expressions.pipe(greedy), Expressions.pipe(caseSensitive));
}

@Override
public boolean foldable() {
return source.foldable() && left.foldable() && right.foldable() && greedy.foldable() && caseSensitive.foldable();
return input.foldable() && left.foldable() && right.foldable() && greedy.foldable() && caseSensitive.foldable();
}

@Override
public Object fold() {
return doProcess(source.fold(), left.fold(), right.fold(), greedy.fold(), caseSensitive.fold());
return doProcess(input.fold(), left.fold(), right.fold(), greedy.fold(), caseSensitive.fold());
}

@Override
protected NodeInfo<? extends Expression> info() {
return NodeInfo.create(this, Between::new, source, left, right, greedy, caseSensitive);
return NodeInfo.create(this, Between::new, input, left, right, greedy, caseSensitive);
}

@Override
public ScriptTemplate asScript() {
ScriptTemplate sourceScript = asScript(source);
ScriptTemplate inputScript = asScript(input);
ScriptTemplate leftScript = asScript(left);
ScriptTemplate rightScript = asScript(right);
ScriptTemplate greedyScript = asScript(greedy);
ScriptTemplate caseSensitiveScript = asScript(caseSensitive);

return asScriptFrom(sourceScript, leftScript, rightScript, greedyScript, caseSensitiveScript);
return asScriptFrom(inputScript, leftScript, rightScript, greedyScript, caseSensitiveScript);
}

protected ScriptTemplate asScriptFrom(ScriptTemplate sourceScript, ScriptTemplate leftScript,
protected ScriptTemplate asScriptFrom(ScriptTemplate inputScript, ScriptTemplate leftScript,
ScriptTemplate rightScript, ScriptTemplate greedyScript, ScriptTemplate caseSensitiveScript) {
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{eql}.%s(%s,%s,%s,%s,%s)"),
"between",
sourceScript.template(),
inputScript.template(),
leftScript.template(),
rightScript.template(),
greedyScript.template(),
caseSensitiveScript.template()),
paramsBuilder()
.script(sourceScript.params())
.script(inputScript.params())
.script(leftScript.params())
.script(rightScript.params())
.script(greedyScript.params())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

public class BetweenFunctionPipe extends Pipe {

private final Pipe source, left, right, greedy, caseSensitive;
private final Pipe input, left, right, greedy, caseSensitive;

public BetweenFunctionPipe(Source source, Expression expression, Pipe src, Pipe left, Pipe right, Pipe greedy, Pipe caseSensitive) {
super(source, expression, Arrays.asList(src, left, right, greedy, caseSensitive));
this.source = src;
public BetweenFunctionPipe(Source source, Expression expression, Pipe input, Pipe left, Pipe right, Pipe greedy, Pipe caseSensitive) {
super(source, expression, Arrays.asList(input, left, right, greedy, caseSensitive));
this.input = input;
this.left = left;
this.right = right;
this.greedy = greedy;
Expand All @@ -38,35 +38,35 @@ public final Pipe replaceChildren(List<Pipe> newChildren) {

@Override
public final Pipe resolveAttributes(AttributeResolver resolver) {
Pipe newSource = source.resolveAttributes(resolver);
Pipe newInput = input.resolveAttributes(resolver);
Pipe newLeft = left.resolveAttributes(resolver);
Pipe newRight = right.resolveAttributes(resolver);
Pipe newGreedy = greedy.resolveAttributes(resolver);
Pipe newCaseSensitive = caseSensitive.resolveAttributes(resolver);
if (newSource == source && newLeft == left && newRight == right && newGreedy == greedy && newCaseSensitive == caseSensitive) {
if (newInput == input && newLeft == left && newRight == right && newGreedy == greedy && newCaseSensitive == caseSensitive) {
return this;
}
return replaceChildren(newSource, newLeft, newRight, newGreedy, newCaseSensitive);
return replaceChildren(newInput, newLeft, newRight, newGreedy, newCaseSensitive);
}

@Override
public boolean supportedByAggsOnlyQuery() {
return source.supportedByAggsOnlyQuery() && left.supportedByAggsOnlyQuery() && right.supportedByAggsOnlyQuery()
return input.supportedByAggsOnlyQuery() && left.supportedByAggsOnlyQuery() && right.supportedByAggsOnlyQuery()
&& greedy.supportedByAggsOnlyQuery() && caseSensitive.supportedByAggsOnlyQuery();
}

@Override
public boolean resolved() {
return source.resolved() && left.resolved() && right.resolved() && greedy.resolved() && caseSensitive.resolved();
return input.resolved() && left.resolved() && right.resolved() && greedy.resolved() && caseSensitive.resolved();
}

protected Pipe replaceChildren(Pipe newSource, Pipe newLeft, Pipe newRight, Pipe newGreedy, Pipe newCaseSensitive) {
return new BetweenFunctionPipe(source(), expression(), newSource, newLeft, newRight, newGreedy, newCaseSensitive);
protected Pipe replaceChildren(Pipe newInput, Pipe newLeft, Pipe newRight, Pipe newGreedy, Pipe newCaseSensitive) {
return new BetweenFunctionPipe(source(), expression(), newInput, newLeft, newRight, newGreedy, newCaseSensitive);
}

@Override
public final void collectFields(QlSourceBuilder sourceBuilder) {
source.collectFields(sourceBuilder);
input.collectFields(sourceBuilder);
left.collectFields(sourceBuilder);
right.collectFields(sourceBuilder);
greedy.collectFields(sourceBuilder);
Expand All @@ -75,17 +75,17 @@ public final void collectFields(QlSourceBuilder sourceBuilder) {

@Override
protected NodeInfo<BetweenFunctionPipe> info() {
return NodeInfo.create(this, BetweenFunctionPipe::new, expression(), source, left, right, greedy, caseSensitive);
return NodeInfo.create(this, BetweenFunctionPipe::new, expression(), input, left, right, greedy, caseSensitive);
}

@Override
public BetweenFunctionProcessor asProcessor() {
return new BetweenFunctionProcessor(source.asProcessor(), left.asProcessor(), right.asProcessor(),
return new BetweenFunctionProcessor(input.asProcessor(), left.asProcessor(), right.asProcessor(),
greedy.asProcessor(), caseSensitive.asProcessor());
}

public Pipe src() {
return source;
public Pipe input() {
return input;
}

public Pipe left() {
Expand All @@ -106,7 +106,7 @@ public Pipe caseSensitive() {

@Override
public int hashCode() {
return Objects.hash(source(), left(), right(), greedy(), caseSensitive());
return Objects.hash(input(), left(), right(), greedy(), caseSensitive());
}

@Override
Expand All @@ -120,7 +120,7 @@ public boolean equals(Object obj) {
}

BetweenFunctionPipe other = (BetweenFunctionPipe) obj;
return Objects.equals(source(), other.source())
return Objects.equals(input(), other.input())
&& Objects.equals(left(), other.left())
&& Objects.equals(right(), other.right())
&& Objects.equals(greedy(), other.greedy())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ public class BetweenFunctionProcessor implements Processor {

public static final String NAME = "sbtw";

private final Processor source, left, right, greedy, caseSensitive;
private final Processor input, left, right, greedy, caseSensitive;

public BetweenFunctionProcessor(Processor source, Processor left, Processor right, Processor greedy, Processor caseSensitive) {
this.source = source;
public BetweenFunctionProcessor(Processor input, Processor left, Processor right, Processor greedy, Processor caseSensitive) {
this.input = input;
this.left = left;
this.right = right;
this.greedy = greedy;
this.caseSensitive = caseSensitive;
}

public BetweenFunctionProcessor(StreamInput in) throws IOException {
source = in.readNamedWriteable(Processor.class);
input = in.readNamedWriteable(Processor.class);
left = in.readNamedWriteable(Processor.class);
right = in.readNamedWriteable(Processor.class);
greedy = in.readNamedWriteable(Processor.class);
Expand All @@ -37,7 +37,7 @@ public BetweenFunctionProcessor(StreamInput in) throws IOException {

@Override
public final void writeTo(StreamOutput out) throws IOException {
out.writeNamedWriteable(source);
out.writeNamedWriteable(input);
out.writeNamedWriteable(left);
out.writeNamedWriteable(right);
out.writeNamedWriteable(greedy);
Expand All @@ -50,33 +50,32 @@ public String getWriteableName() {
}

@Override
public Object process(Object input) {
return doProcess(source.process(input), left.process(input), right.process(input),
greedy.process(input), caseSensitive.process(input));
public Object process(Object o) {
return doProcess(input.process(o), left.process(o), right.process(o), greedy.process(o), caseSensitive.process(o));
}

public static Object doProcess(Object source, Object left, Object right, Object greedy, Object caseSensitive) {
if (source == null) {
public static Object doProcess(Object input, Object left, Object right, Object greedy, Object caseSensitive) {
if (input == null) {
return null;
}

Check.isString(source);
Check.isString(input);
Check.isString(left);
Check.isString(right);

Check.isBoolean(greedy);
Check.isBoolean(caseSensitive);

String str = source.toString();
String str = input.toString();
String strRight = right.toString();
String strLeft = left.toString();
boolean bGreedy = ((Boolean) greedy).booleanValue();
boolean bCaseSensitive = ((Boolean) caseSensitive).booleanValue();
return StringUtils.between(str, strLeft, strRight, bGreedy, bCaseSensitive);
}

protected Processor source() {
return source;
protected Processor input() {
return input;
}

public Processor left() {
Expand All @@ -97,7 +96,7 @@ public Processor caseSensitive() {

@Override
public int hashCode() {
return Objects.hash(source(), left(), right(), greedy(), caseSensitive());
return Objects.hash(input(), left(), right(), greedy(), caseSensitive());
}

@Override
Expand All @@ -111,7 +110,7 @@ public boolean equals(Object obj) {
}

BetweenFunctionProcessor other = (BetweenFunctionProcessor) obj;
return Objects.equals(source(), other.source())
return Objects.equals(input(), other.input())
&& Objects.equals(left(), other.left())
&& Objects.equals(right(), other.right())
&& Objects.equals(greedy(), other.greedy())
Expand Down
Loading

0 comments on commit 0f83d57

Please sign in to comment.