Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Objects;
import lombok.Getter;
import org.opensearch.sql.expression.NamedExpression;
import org.opensearch.sql.planner.PlanContext;

/**
* The context used for Analyzer.
Expand All @@ -23,13 +24,28 @@ public class AnalysisContext {
@Getter
private final List<NamedExpression> namedParseExpressions;

/**
* Context for physical plan building.
*/
@Getter
private PlanContext planContext;

public AnalysisContext() {
this(new TypeEnvironment(null));
this(new TypeEnvironment(null), new PlanContext());
}

public AnalysisContext(PlanContext planContext) {
this(new TypeEnvironment(null), planContext);
}

public AnalysisContext(TypeEnvironment environment) {
this(environment, new PlanContext());
}

public AnalysisContext(TypeEnvironment environment, PlanContext planContext) {
this.environment = environment;
this.namedParseExpressions = new ArrayList<>();
this.planContext = planContext;
}

/**
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/java/org/opensearch/sql/analysis/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.opensearch.sql.expression.ReferenceExpression;
import org.opensearch.sql.expression.aggregation.Aggregator;
import org.opensearch.sql.expression.aggregation.NamedAggregator;
import org.opensearch.sql.planner.IndexScanType;
import org.opensearch.sql.planner.logical.LogicalAD;
import org.opensearch.sql.planner.logical.LogicalAggregation;
import org.opensearch.sql.planner.logical.LogicalDedupe;
Expand Down Expand Up @@ -241,6 +242,10 @@ public LogicalPlan visitRareTopN(RareTopN node, AnalysisContext context) {
}
ImmutableList<Expression> fields = fieldsBuilder.build();

if (node.getCommandType() == RareTopN.CommandType.TOPOFALL) {
context.getPlanContext().setIndexScanType(IndexScanType.FETCH_ALL);
}

// new context
context.push();
TypeEnvironment newEnv = context.peek();
Expand All @@ -252,7 +257,11 @@ public LogicalPlan visitRareTopN(RareTopN node, AnalysisContext context) {
List<Argument> options = node.getNoOfResults();
Integer noOfResults = (Integer) options.get(0).getValue().getValue();

return new LogicalRareTopN(child, node.getCommandType(), noOfResults, fields, groupBys);
if (node.getCommandType() != RareTopN.CommandType.TOPOFALL) {
return new LogicalRareTopN(child, node.getCommandType(), noOfResults, fields, groupBys);
} else {
return new LogicalRareTopN(child, RareTopN.CommandType.TOP, noOfResults, fields, groupBys);

@seankao-az seankao-az Jul 20, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only made minimal changes for the RareTopN to make topOfAll work, so here it may look ugly. There's no LogicalRareTopN for topOfAll. topOfAll is treated as a top command logically. They have the same logical plan and differ only in the planContext. This leads to different physical plan generation.
The RareTopN.CommandType.TOPOFALL is merely for this class to know whether to setIndexScanType as in line 245

}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) {

public enum CommandType {
TOP,
RARE
RARE,
TOPOFALL
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.planner;

/**
* Enum representing supported index scan modes
*/
public enum IndexScanType {

/**
* default regular request
*/
DEFAULT,

/**
* scroll request
*/
SCROLL,

/**
* fetch all data
*/
FETCH_ALL
}
20 changes: 20 additions & 0 deletions core/src/main/java/org/opensearch/sql/planner/PlanContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.planner;

import lombok.Getter;
import lombok.Setter;

public class PlanContext {
@Getter
@Setter
private IndexScanType indexScanType;

public PlanContext() {
this.indexScanType = IndexScanType.DEFAULT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.opensearch.sql.planner.logical.LogicalPlan;
import org.opensearch.sql.planner.logical.LogicalPlanNodeVisitor;
import org.opensearch.sql.planner.logical.LogicalRelation;
Expand Down Expand Up @@ -40,14 +41,19 @@ public class Planner {
* @return optimal physical plan
*/
public PhysicalPlan plan(LogicalPlan plan) {
return plan(plan, new PlanContext());
}

public PhysicalPlan plan(LogicalPlan plan, PlanContext context) {
String tableName = findTableName(plan);
if (isNullOrEmpty(tableName)) {
return plan.accept(new DefaultImplementor<>(), null);
}

Table table = storageEngine.getTable(tableName);
return table.implement(
table.optimize(optimize(plan)));
table.optimize(optimize(plan)),
context);
}

private String findTableName(LogicalPlan plan) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/opensearch/sql/storage/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.Map;
import org.opensearch.sql.data.type.ExprType;
import org.opensearch.sql.planner.PlanContext;
import org.opensearch.sql.planner.logical.LogicalPlan;
import org.opensearch.sql.planner.physical.PhysicalPlan;

Expand All @@ -28,6 +29,7 @@ public interface Table {
* @return physical plan
*/
PhysicalPlan implement(LogicalPlan plan);
PhysicalPlan implement(LogicalPlan plan, PlanContext context);

/**
* Optimize the {@link LogicalPlan} by storage engine rule.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.opensearch.sql.opensearch.storage.script.sort.SortQueryBuilder;
import org.opensearch.sql.opensearch.storage.serialization.DefaultExpressionSerializer;
import org.opensearch.sql.planner.DefaultImplementor;
import org.opensearch.sql.planner.PlanContext;
import org.opensearch.sql.planner.logical.LogicalAD;
import org.opensearch.sql.planner.logical.LogicalMLCommons;
import org.opensearch.sql.planner.logical.LogicalPlan;
Expand Down Expand Up @@ -84,7 +85,12 @@ public Map<String, ExprType> getFieldTypes() {
*/
@Override
public PhysicalPlan implement(LogicalPlan plan) {
OpenSearchIndexScan indexScan = new OpenSearchIndexScan(client, settings, indexName,
return implement(plan, new PlanContext());
}

@Override
public PhysicalPlan implement(LogicalPlan plan, PlanContext context) {
OpenSearchIndexScan indexScan = new OpenSearchIndexScan(client, settings, indexName, context,
new OpenSearchExprValueFactory(getFieldTypes()));

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@
import org.opensearch.sql.opensearch.data.value.OpenSearchExprValueFactory;
import org.opensearch.sql.opensearch.request.OpenSearchQueryRequest;
import org.opensearch.sql.opensearch.request.OpenSearchRequest;
import org.opensearch.sql.opensearch.request.OpenSearchScrollRequest;
import org.opensearch.sql.opensearch.response.OpenSearchResponse;
import org.opensearch.sql.opensearch.response.agg.OpenSearchAggregationResponseParser;
import org.opensearch.sql.planner.IndexScanType;
import org.opensearch.sql.planner.PlanContext;
import org.opensearch.sql.planner.Planner;
import org.opensearch.sql.storage.TableScanOperator;

/**
Expand Down Expand Up @@ -63,18 +67,46 @@ public class OpenSearchIndexScan extends TableScanOperator {
public OpenSearchIndexScan(OpenSearchClient client,
Settings settings, String indexName,
OpenSearchExprValueFactory exprValueFactory) {
this(client, settings, new OpenSearchRequest.IndexName(indexName), exprValueFactory);
this(client, settings, new OpenSearchRequest.IndexName(indexName), new PlanContext(), exprValueFactory);
}

/**
* Constructor.
*/
public OpenSearchIndexScan(OpenSearchClient client, Settings settings,
String indexName, PlanContext context,
OpenSearchExprValueFactory exprValueFactory) {
this(client, settings, new OpenSearchRequest.IndexName(indexName), context, exprValueFactory);
}
/**
* Constructor.
*/
public OpenSearchIndexScan(OpenSearchClient client,
Settings settings, OpenSearchRequest.IndexName indexName,
OpenSearchExprValueFactory exprValueFactory) {
Settings settings, OpenSearchRequest.IndexName indexName,
OpenSearchExprValueFactory exprValueFactory) {
this(client, settings, indexName, new PlanContext(), exprValueFactory);
}

/**
* Constructor.
*/
public OpenSearchIndexScan(OpenSearchClient client, Settings settings,
OpenSearchRequest.IndexName indexName, PlanContext context,
OpenSearchExprValueFactory exprValueFactory) {
// TODO: context
this.client = client;
this.request = new OpenSearchQueryRequest(indexName,
settings.getSettingValue(Settings.Key.QUERY_SIZE_LIMIT), exprValueFactory);
switch (context.getIndexScanType()) {
case FETCH_ALL:
this.request = new OpenSearchScrollRequest(indexName, exprValueFactory);
break;
case SCROLL:
// TODO
case DEFAULT:
default:
this.request = new OpenSearchQueryRequest(indexName,
settings.getSettingValue(Settings.Key.QUERY_SIZE_LIMIT), exprValueFactory);
break;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import com.google.common.annotations.VisibleForTesting;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.apache.commons.lang3.tuple.Pair;
import org.opensearch.sql.data.type.ExprType;
import org.opensearch.sql.opensearch.client.OpenSearchClient;
import org.opensearch.sql.opensearch.request.system.OpenSearchCatIndicesRequest;
import org.opensearch.sql.opensearch.request.system.OpenSearchDescribeIndexRequest;
import org.opensearch.sql.opensearch.request.system.OpenSearchSystemRequest;
import org.opensearch.sql.planner.DefaultImplementor;
import org.opensearch.sql.planner.PlanContext;
import org.opensearch.sql.planner.logical.LogicalPlan;
import org.opensearch.sql.planner.logical.LogicalRelation;
import org.opensearch.sql.planner.physical.PhysicalPlan;
Expand Down Expand Up @@ -48,6 +50,12 @@ public PhysicalPlan implement(LogicalPlan plan) {
return plan.accept(new OpenSearchSystemIndexDefaultImplementor(), null);
}

@Override
public PhysicalPlan implement(LogicalPlan plan, PlanContext context) {
// context is of no use for system index scan
return implement(plan);
}

@VisibleForTesting
@RequiredArgsConstructor
public class OpenSearchSystemIndexDefaultImplementor
Expand Down
1 change: 1 addition & 0 deletions ppl/src/main/antlr/OpenSearchPPLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ RARE: 'RARE';
PARSE: 'PARSE';
KMEANS: 'KMEANS';
AD: 'AD';
TOPOFALL: 'TOPOFALL';

// COMMAND ASSIST KEYWORDS
AS: 'AS';
Expand Down
9 changes: 8 additions & 1 deletion ppl/src/main/antlr/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pplCommands

commands
: whereCommand | fieldsCommand | renameCommand | statsCommand | dedupCommand | sortCommand | evalCommand | headCommand
| topCommand | rareCommand | parseCommand | kmeansCommand | adCommand;
| topCommand | rareCommand | parseCommand | kmeansCommand | adCommand | topOfAllCommand;

searchCommand
: (SEARCH)? fromClause #searchFrom
Expand Down Expand Up @@ -81,6 +81,13 @@ headCommand
(FROM from=integerLiteral)?
;

topOfAllCommand
: TOPOFALL
(number=integerLiteral)?
fieldList
(byClause)?
;

topCommand
: TOP
(number=integerLiteral)?
Expand Down
6 changes: 4 additions & 2 deletions ppl/src/main/java/org/opensearch/sql/ppl/PPLService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.opensearch.sql.executor.ExecutionEngine.ExplainResponse;
import org.opensearch.sql.expression.DSL;
import org.opensearch.sql.expression.function.BuiltinFunctionRepository;
import org.opensearch.sql.planner.PlanContext;
import org.opensearch.sql.planner.Planner;
import org.opensearch.sql.planner.logical.LogicalPlan;
import org.opensearch.sql.planner.optimizer.LogicalPlanOptimizer;
Expand Down Expand Up @@ -87,12 +88,13 @@ private PhysicalPlan plan(PPLQueryRequest request) {
LOG.info("[{}] Incoming request {}", LogUtils.getRequestId(), anonymizer.anonymizeData(ast));

// 2.Analyze abstract syntax to generate logical plan
PlanContext planContext = new PlanContext();
LogicalPlan logicalPlan = analyzer.analyze(UnresolvedPlanHelper.addSelectAll(ast),
new AnalysisContext());
new AnalysisContext(planContext));

// 3.Generate optimal physical plan from logical plan
return new Planner(storageEngine, LogicalPlanOptimizer.create(new DSL(repository)))
.plan(logicalPlan);
.plan(logicalPlan, planContext);
}

}
13 changes: 13 additions & 0 deletions ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.StatsCommandContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.TableSourceClauseContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.TopCommandContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.TopOfAllCommandContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.WhereCommandContext;
import static org.opensearch.sql.utils.SystemIndexUtils.mappingTable;

Expand Down Expand Up @@ -294,6 +295,18 @@ public UnresolvedPlan visitTopCommand(TopCommandContext ctx) {
);
}

@Override
public UnresolvedPlan visitTopOfAllCommand(TopOfAllCommandContext ctx) {
List<UnresolvedExpression> groupList = ctx.byClause() == null ? Collections.emptyList() :
getGroupByList(ctx.byClause());
return new RareTopN(
CommandType.TOPOFALL,
ArgumentFactory.getArgumentList(ctx),
getFieldList(ctx.fieldList()),
groupList
);
}

/**
* From clause.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.SortFieldContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.StatsCommandContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.TopCommandContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.TopOfAllCommandContext;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -124,6 +125,14 @@ public static List<Argument> getArgumentList(TopCommandContext ctx) {
);
}

public static List<Argument> getArgumentList(TopOfAllCommandContext ctx) {
return Collections.singletonList(
ctx.number != null
? new Argument("noOfResults", getArgumentValue(ctx.number))
: new Argument("noOfResults", new Literal(10, DataType.INTEGER))
);
}

/**
* Get list of {@link Argument}.
*
Expand Down