Skip to content

Commit 31caf30

Browse files
committed
add analyzer test
1 parent aa74bfd commit 31caf30

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/AnalyzerTests.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.xpack.esql.core.expression.Attribute;
2323
import org.elasticsearch.xpack.esql.core.expression.AttributeSet;
2424
import org.elasticsearch.xpack.esql.core.expression.EntryExpression;
25+
import org.elasticsearch.xpack.esql.core.expression.Expression;
2526
import org.elasticsearch.xpack.esql.core.expression.Expressions;
2627
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
2728
import org.elasticsearch.xpack.esql.core.expression.Literal;
@@ -37,6 +38,7 @@
3738
import org.elasticsearch.xpack.esql.expression.function.aggregate.Min;
3839
import org.elasticsearch.xpack.esql.expression.function.fulltext.Match;
3940
import org.elasticsearch.xpack.esql.expression.function.fulltext.MatchOperator;
41+
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.GreaterThan;
4042
import org.elasticsearch.xpack.esql.index.EsIndex;
4143
import org.elasticsearch.xpack.esql.index.IndexResolution;
4244
import org.elasticsearch.xpack.esql.parser.ParsingException;
@@ -50,6 +52,7 @@
5052
import org.elasticsearch.xpack.esql.plan.logical.Limit;
5153
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
5254
import org.elasticsearch.xpack.esql.plan.logical.Lookup;
55+
import org.elasticsearch.xpack.esql.plan.logical.Merge;
5356
import org.elasticsearch.xpack.esql.plan.logical.OrderBy;
5457
import org.elasticsearch.xpack.esql.plan.logical.Row;
5558
import org.elasticsearch.xpack.esql.plan.logical.UnresolvedRelation;
@@ -85,6 +88,7 @@
8588
import static org.elasticsearch.xpack.esql.core.tree.Source.EMPTY;
8689
import static org.hamcrest.Matchers.contains;
8790
import static org.hamcrest.Matchers.containsString;
91+
import static org.hamcrest.Matchers.empty;
8892
import static org.hamcrest.Matchers.equalTo;
8993
import static org.hamcrest.Matchers.hasItem;
9094
import static org.hamcrest.Matchers.hasSize;
@@ -2587,6 +2591,60 @@ public void testFunctionNamedParamsAsFunctionArgument() {
25872591
assertEquals(DataType.DOUBLE, ee.dataType());
25882592
}
25892593

2594+
public void testBasicFork() {
2595+
assumeTrue("requires FORK capability", EsqlCapabilities.Cap.FORK.isEnabled());
2596+
2597+
LogicalPlan plan = analyze("""
2598+
from test*
2599+
| WHERE x > 1
2600+
| FORK [ WHERE b > 2 ]
2601+
[ WHERE C > 3 ]
2602+
[ WHERE d > 4 | SORT x | LIMIT 7 ]
2603+
""");
2604+
2605+
Limit limit = as(plan, Limit.class);
2606+
Merge merge = as(limit.child(), Merge.class);
2607+
assertThat(merge.children(), empty());
2608+
2609+
// fork branch 1
2610+
limit = as(merge.subPlans().get(0), Limit.class);
2611+
assertThat(as(limit.limit(), Literal.class).value(), equalTo(DEFAULT_LIMIT));
2612+
Eval eval = as(limit.child(), Eval.class);
2613+
assertThat(as(eval.fields().get(0), Alias.class), equalTo(alias("_fork", string("fork1"))));
2614+
Filter filter = as(eval.child(), Filter.class);
2615+
assertThat(as(filter.condition(), GreaterThan.class), equalTo(greaterThan(attribute("b"), literal(2))));
2616+
filter = as(filter.child(), Filter.class);
2617+
assertThat(as(filter.condition(), GreaterThan.class), equalTo(greaterThan(attribute("x"), literal(1))));
2618+
var esRelation = as(filter.child(), EsRelation.class);
2619+
assertThat(esRelation.indexPattern(), equalTo("test"));
2620+
2621+
// fork branch 2
2622+
limit = as(merge.subPlans().get(1), Limit.class);
2623+
assertThat(as(limit.limit(), Literal.class).value(), equalTo(DEFAULT_LIMIT));
2624+
eval = as(limit.child(), Eval.class);
2625+
assertThat(as(eval.fields().get(0), Alias.class), equalTo(alias("_fork", string("fork2"))));
2626+
filter = as(eval.child(), Filter.class);
2627+
assertThat(as(filter.condition(), GreaterThan.class), equalTo(greaterThan(attribute("C"), literal(3))));
2628+
filter = as(filter.child(), Filter.class);
2629+
assertThat(as(filter.condition(), GreaterThan.class), equalTo(greaterThan(attribute("x"), literal(1))));
2630+
esRelation = as(filter.child(), EsRelation.class);
2631+
assertThat(esRelation.indexPattern(), equalTo("test"));
2632+
2633+
// fork branch 3
2634+
limit = as(merge.subPlans().get(2), Limit.class);
2635+
assertThat(as(limit.limit(), Literal.class).value(), equalTo(MAX_LIMIT));
2636+
eval = as(limit.child(), Eval.class);
2637+
assertThat(as(eval.fields().get(0), Alias.class), equalTo(alias("_fork", string("fork3"))));
2638+
limit = as(eval.child(), Limit.class);
2639+
var orderBy = as(limit.child(), OrderBy.class);
2640+
filter = as(orderBy.child(), Filter.class);
2641+
assertThat(as(filter.condition(), GreaterThan.class), equalTo(greaterThan(attribute("d"), literal(4))));
2642+
filter = as(filter.child(), Filter.class);
2643+
assertThat(as(filter.condition(), GreaterThan.class), equalTo(greaterThan(attribute("x"), literal(1))));
2644+
esRelation = as(filter.child(), EsRelation.class);
2645+
assertThat(esRelation.indexPattern(), equalTo("test"));
2646+
}
2647+
25902648
private void verifyUnsupported(String query, String errorMessage) {
25912649
verifyUnsupported(query, errorMessage, "mapping-multi-field-variation.json");
25922650
}
@@ -2653,4 +2711,24 @@ private void assertEmptyEsRelation(LogicalPlan plan) {
26532711
protected IndexAnalyzers createDefaultIndexAnalyzers() {
26542712
return super.createDefaultIndexAnalyzers();
26552713
}
2714+
2715+
static GreaterThan greaterThan(Expression left, Expression right) {
2716+
return new GreaterThan(EMPTY, left, right);
2717+
}
2718+
2719+
static UnresolvedAttribute attribute(String name) {
2720+
return new UnresolvedAttribute(EMPTY, name);
2721+
}
2722+
2723+
static Alias alias(String name, Expression value) {
2724+
return new Alias(EMPTY, name, value);
2725+
}
2726+
2727+
static Literal string(String value) {
2728+
return new Literal(EMPTY, value, DataType.KEYWORD);
2729+
}
2730+
2731+
static Literal literal(int value) {
2732+
return new Literal(EMPTY, value, DataType.INTEGER);
2733+
}
26562734
}

0 commit comments

Comments
 (0)