Skip to content

Commit 26900bf

Browse files
committed
EQL: Add infra for planning and query folding (#52065)
Actual folding not yet in place (TBD) (cherry picked from commit d52b96f)
1 parent e098e83 commit 26900bf

File tree

24 files changed

+977
-21
lines changed

24 files changed

+977
-21
lines changed

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Analyzer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package org.elasticsearch.xpack.eql.analysis;
88

9+
import org.elasticsearch.xpack.eql.common.Failure;
910
import org.elasticsearch.xpack.ql.expression.Attribute;
1011
import org.elasticsearch.xpack.ql.expression.NamedExpression;
1112
import org.elasticsearch.xpack.ql.expression.UnresolvedAttribute;

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/VerificationException.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,14 @@
77

88
import org.elasticsearch.rest.RestStatus;
99
import org.elasticsearch.xpack.eql.EqlClientException;
10-
import org.elasticsearch.xpack.ql.tree.Location;
11-
import org.elasticsearch.xpack.ql.util.StringUtils;
10+
import org.elasticsearch.xpack.eql.common.Failure;
1211

1312
import java.util.Collection;
14-
import java.util.stream.Collectors;
1513

1614
public class VerificationException extends EqlClientException {
1715

1816
protected VerificationException(Collection<Failure> sources) {
19-
super(asMessage(sources));
20-
}
21-
22-
private static String asMessage(Collection<Failure> failures) {
23-
return failures.stream().map(f -> {
24-
Location l = f.node().source().source();
25-
return "line " + l.getLineNumber() + ":" + l.getColumnNumber() + ": " + f.message();
26-
}).collect(Collectors.joining(StringUtils.NEW_LINE, "Found " + failures.size() + " problem(s)\n", StringUtils.EMPTY));
17+
super(Failure.failMessage(sources));
2718
}
2819

2920
@Override

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Verifier.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package org.elasticsearch.xpack.eql.analysis;
88

9+
import org.elasticsearch.xpack.eql.common.Failure;
910
import org.elasticsearch.xpack.ql.capabilities.Unresolvable;
1011
import org.elasticsearch.xpack.ql.expression.Attribute;
1112
import org.elasticsearch.xpack.ql.expression.UnresolvedAttribute;
@@ -22,7 +23,7 @@
2223
import java.util.Set;
2324

2425
import static java.util.stream.Collectors.toMap;
25-
import static org.elasticsearch.xpack.eql.analysis.Failure.fail;
26+
import static org.elasticsearch.xpack.eql.common.Failure.fail;
2627

2728
/**
2829
* The verifier has the role of checking the analyzed tree for failures and build a list of failures following this check.

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Failure.java renamed to x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/common/Failure.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,33 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
package org.elasticsearch.xpack.eql.analysis;
7+
package org.elasticsearch.xpack.eql.common;
88

9+
import org.elasticsearch.xpack.ql.tree.Location;
910
import org.elasticsearch.xpack.ql.tree.Node;
11+
import org.elasticsearch.xpack.ql.util.StringUtils;
1012

13+
import java.util.Collection;
1114
import java.util.Objects;
15+
import java.util.stream.Collectors;
1216

1317
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
1418

15-
class Failure {
19+
public class Failure {
1620

1721
private final Node<?> node;
1822
private final String message;
1923

20-
Failure(Node<?> node, String message) {
24+
public Failure(Node<?> node, String message) {
2125
this.node = node;
2226
this.message = message;
2327
}
2428

25-
Node<?> node() {
29+
public Node<?> node() {
2630
return node;
2731
}
2832

29-
String message() {
33+
public String message() {
3034
return message;
3135
}
3236

@@ -54,7 +58,16 @@ public String toString() {
5458
return message;
5559
}
5660

57-
static Failure fail(Node<?> source, String message, Object... args) {
61+
public static Failure fail(Node<?> source, String message, Object... args) {
5862
return new Failure(source, format(message, args));
5963
}
64+
65+
public static String failMessage(Collection<Failure> failures) {
66+
return failures.stream().map(f -> {
67+
Location l = f.node().source().source();
68+
return "line " + l.getLineNumber() + ":" + l.getColumnNumber() + ": " + f.message();
69+
}).collect(Collectors.joining(StringUtils.NEW_LINE,
70+
format("Found {} problem{}\n", failures.size(), failures.size() > 1 ? "s" : StringUtils.EMPTY),
71+
StringUtils.EMPTY));
72+
}
6073
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.eql.plan.physical;
7+
8+
import org.elasticsearch.action.ActionListener;
9+
import org.elasticsearch.xpack.eql.querydsl.container.QueryContainer;
10+
import org.elasticsearch.xpack.eql.session.EqlSession;
11+
import org.elasticsearch.xpack.eql.session.Results;
12+
import org.elasticsearch.xpack.ql.expression.Attribute;
13+
import org.elasticsearch.xpack.ql.tree.NodeInfo;
14+
import org.elasticsearch.xpack.ql.tree.Source;
15+
16+
import java.util.List;
17+
import java.util.Objects;
18+
19+
public class EsQueryExec extends LeafExec {
20+
21+
private final String index;
22+
private final List<Attribute> output;
23+
private final QueryContainer queryContainer;
24+
25+
public EsQueryExec(Source source, String index, List<Attribute> output, QueryContainer queryContainer) {
26+
super(source);
27+
this.index = index;
28+
this.output = output;
29+
this.queryContainer = queryContainer;
30+
}
31+
32+
@Override
33+
protected NodeInfo<EsQueryExec> info() {
34+
return NodeInfo.create(this, EsQueryExec::new, index, output, queryContainer);
35+
}
36+
37+
public EsQueryExec with(QueryContainer queryContainer) {
38+
return new EsQueryExec(source(), index, output, queryContainer);
39+
}
40+
41+
public String index() {
42+
return index;
43+
}
44+
45+
@Override
46+
public List<Attribute> output() {
47+
return output;
48+
}
49+
50+
@Override
51+
public void execute(EqlSession session, ActionListener<Results> listener) {
52+
throw new UnsupportedOperationException();
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return Objects.hash(index, queryContainer, output);
58+
}
59+
60+
@Override
61+
public boolean equals(Object obj) {
62+
if (this == obj) {
63+
return true;
64+
}
65+
66+
if (obj == null || getClass() != obj.getClass()) {
67+
return false;
68+
}
69+
70+
EsQueryExec other = (EsQueryExec) obj;
71+
return Objects.equals(index, other.index)
72+
&& Objects.equals(queryContainer, other.queryContainer)
73+
&& Objects.equals(output, other.output);
74+
}
75+
76+
@Override
77+
public String nodeString() {
78+
return nodeName() + "[" + index + "," + queryContainer + "]";
79+
}
80+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.eql.plan.physical;
7+
8+
import org.elasticsearch.xpack.ql.expression.Attribute;
9+
import org.elasticsearch.xpack.ql.expression.Expression;
10+
import org.elasticsearch.xpack.ql.tree.NodeInfo;
11+
import org.elasticsearch.xpack.ql.tree.Source;
12+
13+
import java.util.List;
14+
import java.util.Objects;
15+
16+
public class FilterExec extends UnaryExec implements Unexecutable {
17+
18+
private final Expression condition;
19+
private final boolean onAggs;
20+
21+
public FilterExec(Source source, PhysicalPlan child, Expression condition) {
22+
this(source, child, condition, false);
23+
}
24+
25+
public FilterExec(Source source, PhysicalPlan child, Expression condition, boolean onAggs) {
26+
super(source, child);
27+
this.condition = condition;
28+
this.onAggs = onAggs;
29+
}
30+
31+
@Override
32+
protected NodeInfo<FilterExec> info() {
33+
return NodeInfo.create(this, FilterExec::new, child(), condition, onAggs);
34+
}
35+
36+
@Override
37+
protected FilterExec replaceChild(PhysicalPlan newChild) {
38+
return new FilterExec(source(), newChild, condition, onAggs);
39+
}
40+
41+
public Expression condition() {
42+
return condition;
43+
}
44+
45+
public boolean onAggs() {
46+
return onAggs;
47+
}
48+
49+
@Override
50+
public List<Attribute> output() {
51+
return child().output();
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hash(condition, onAggs, child());
57+
}
58+
59+
@Override
60+
public boolean equals(Object obj) {
61+
if (this == obj) {
62+
return true;
63+
}
64+
65+
if (obj == null || getClass() != obj.getClass()) {
66+
return false;
67+
}
68+
69+
FilterExec other = (FilterExec) obj;
70+
return onAggs == other.onAggs
71+
&& Objects.equals(condition, other.condition)
72+
&& Objects.equals(child(), other.child());
73+
}
74+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.eql.plan.physical;
7+
8+
import org.elasticsearch.xpack.ql.tree.Source;
9+
10+
import java.util.Collections;
11+
import java.util.List;
12+
13+
public abstract class LeafExec extends PhysicalPlan {
14+
15+
protected LeafExec(Source source) {
16+
super(source, Collections.emptyList());
17+
}
18+
19+
@Override
20+
public final LeafExec replaceChildren(List<PhysicalPlan> newChildren) {
21+
throw new UnsupportedOperationException("this type of node doesn't have any children to replace");
22+
}
23+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.eql.plan.physical;
7+
8+
import org.elasticsearch.xpack.ql.expression.Expression;
9+
import org.elasticsearch.xpack.ql.tree.NodeInfo;
10+
import org.elasticsearch.xpack.ql.tree.Source;
11+
12+
import java.util.Objects;
13+
14+
public class LimitExec extends UnaryExec implements Unexecutable {
15+
16+
private final Expression limit;
17+
18+
public LimitExec(Source source, PhysicalPlan child, Expression limit) {
19+
super(source, child);
20+
this.limit = limit;
21+
}
22+
23+
@Override
24+
protected NodeInfo<LimitExec> info() {
25+
return NodeInfo.create(this, LimitExec::new, child(), limit);
26+
}
27+
28+
@Override
29+
protected LimitExec replaceChild(PhysicalPlan newChild) {
30+
return new LimitExec(source(), newChild, limit);
31+
}
32+
33+
public Expression limit() {
34+
return limit;
35+
}
36+
37+
@Override
38+
public int hashCode() {
39+
return Objects.hash(limit, child());
40+
}
41+
42+
@Override
43+
public boolean equals(Object obj) {
44+
if (this == obj) {
45+
return true;
46+
}
47+
48+
if (obj == null || getClass() != obj.getClass()) {
49+
return false;
50+
}
51+
52+
LimitExec other = (LimitExec) obj;
53+
return Objects.equals(limit, other.limit)
54+
&& Objects.equals(child(), other.child());
55+
}
56+
}

0 commit comments

Comments
 (0)