Skip to content

Commit

Permalink
Add DefaultImplementor and PhysicalPlanNodeVisitor tests
Browse files Browse the repository at this point in the history
Signed-off-by: James Duong <[email protected]>
  • Loading branch information
jduo committed Oct 27, 2024
1 parent 85e54e2 commit 515d590
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -44,8 +45,10 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.sql.ast.dsl.AstDSL;
import org.opensearch.sql.ast.tree.RareTopN.CommandType;
import org.opensearch.sql.ast.tree.Sort;
import org.opensearch.sql.ast.tree.Trendline;
import org.opensearch.sql.data.model.ExprBooleanValue;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.executor.pagination.PlanSerializer;
Expand All @@ -63,11 +66,13 @@
import org.opensearch.sql.planner.logical.LogicalPlanDSL;
import org.opensearch.sql.planner.logical.LogicalProject;
import org.opensearch.sql.planner.logical.LogicalRelation;
import org.opensearch.sql.planner.logical.LogicalTrendline;
import org.opensearch.sql.planner.logical.LogicalValues;
import org.opensearch.sql.planner.physical.CursorCloseOperator;
import org.opensearch.sql.planner.physical.PhysicalPlan;
import org.opensearch.sql.planner.physical.PhysicalPlanDSL;
import org.opensearch.sql.planner.physical.ProjectOperator;
import org.opensearch.sql.planner.physical.TrendlineOperator;
import org.opensearch.sql.planner.physical.ValuesOperator;
import org.opensearch.sql.storage.StorageEngine;
import org.opensearch.sql.storage.Table;
Expand Down Expand Up @@ -304,4 +309,17 @@ public void visitLimit_support_return_takeOrdered() {
5);
assertEquals(physicalPlanTree, logicalLimit.accept(implementor, null));
}

@Test
public void visitTrendline_should_build_TrendlineOperator() {
var logicalChild = mock(LogicalPlan.class);
var physicalChild = mock(PhysicalPlan.class);
when(logicalChild.accept(implementor, null)).thenReturn(physicalChild);
final Trendline.TrendlineComputation computation =
AstDSL.computation(1, AstDSL.field("field"), "alias", "sma");
var logicalPlan = new LogicalTrendline(logicalChild, Collections.singletonList(computation));
var implemented = logicalPlan.accept(implementor, null);
assertInstanceOf(TrendlineOperator.class, implemented);
assertSame(physicalChild, implemented.getChild().get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -43,6 +44,7 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.sql.ast.dsl.AstDSL;
import org.opensearch.sql.ast.tree.RareTopN.CommandType;
import org.opensearch.sql.ast.tree.Sort.SortOption;
import org.opensearch.sql.expression.DSL;
Expand All @@ -65,7 +67,14 @@ public void print_physical_plan() {
agg(
rareTopN(
filter(
limit(new TestScan(), 1, 1),
limit(
new TrendlineOperator(
new TestScan(),
Collections.singletonList(
AstDSL.computation(
1, AstDSL.field("field"), "alias", "sma"))),
1,
1),
DSL.equal(DSL.ref("response", INTEGER), DSL.literal(10))),
CommandType.TOP,
ImmutableList.of(),
Expand All @@ -85,7 +94,8 @@ public void print_physical_plan() {
+ "\t\t\tAggregation->\n"
+ "\t\t\t\tRareTopN->\n"
+ "\t\t\t\t\tFilter->\n"
+ "\t\t\t\t\t\tLimit->",
+ "\t\t\t\t\t\tLimit->\n"
+ "\t\t\t\t\t\t\tTrendline->",
printer.print(plan));
}

Expand Down Expand Up @@ -134,6 +144,12 @@ public static Stream<Arguments> getPhysicalPlanForTest() {

PhysicalPlan cursorClose = new CursorCloseOperator(plan);

PhysicalPlan trendline =
new TrendlineOperator(
plan,
Collections.singletonList(
AstDSL.computation(1, AstDSL.field("field"), "alias", "sma")));

return Stream.of(
Arguments.of(filter, "filter"),
Arguments.of(aggregation, "aggregation"),
Expand All @@ -149,7 +165,8 @@ public static Stream<Arguments> getPhysicalPlanForTest() {
Arguments.of(rareTopN, "rareTopN"),
Arguments.of(limit, "limit"),
Arguments.of(nested, "nested"),
Arguments.of(cursorClose, "cursorClose"));
Arguments.of(cursorClose, "cursorClose"),
Arguments.of(trendline, "trendline"));
}

@ParameterizedTest(name = "{1}")
Expand Down Expand Up @@ -223,6 +240,11 @@ public String visitLimit(LimitOperator node, Integer tabs) {
return name(node, "Limit->", tabs);
}

@Override
public String visitTrendline(TrendlineOperator node, Integer tabs) {
return name(node, "Trendline->", tabs);
}

private String name(PhysicalPlan node, String current, int tabs) {
String child = node.getChild().get(0).accept(this, tabs + 1);
StringBuilder sb = new StringBuilder();
Expand Down

0 comments on commit 515d590

Please sign in to comment.