Skip to content

Commit

Permalink
Add Statement, QueryExecution and QueryManager (#845)
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Huo <penghuo@gmail.com>
  • Loading branch information
penghuo authored Oct 28, 2022
1 parent 63f3449 commit 30fcd79
Show file tree
Hide file tree
Showing 41 changed files with 1,684 additions and 412 deletions.
15 changes: 15 additions & 0 deletions core/src/main/java/org/opensearch/sql/ast/AbstractNodeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import org.opensearch.sql.ast.expression.When;
import org.opensearch.sql.ast.expression.WindowFunction;
import org.opensearch.sql.ast.expression.Xor;
import org.opensearch.sql.ast.statement.Explain;
import org.opensearch.sql.ast.statement.Query;
import org.opensearch.sql.ast.statement.Statement;
import org.opensearch.sql.ast.tree.AD;
import org.opensearch.sql.ast.tree.Aggregation;
import org.opensearch.sql.ast.tree.Dedupe;
Expand Down Expand Up @@ -269,4 +272,16 @@ public T visitAD(AD node, C context) {
public T visitHighlightFunction(HighlightFunction node, C context) {
return visitChildren(node, context);
}

public T visitStatement(Statement node, C context) {
return visit(node, context);
}

public T visitQuery(Query node, C context) {
return visitStatement(node, context);
}

public T visitExplain(Explain node, C context) {
return visitStatement(node, context);
}
}
26 changes: 26 additions & 0 deletions core/src/main/java/org/opensearch/sql/ast/statement/Explain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.sql.ast.statement;

import lombok.Data;
import org.opensearch.sql.ast.AbstractNodeVisitor;

/**
* Explain Statement.
*/
@Data
public class Explain extends Statement {

private final Statement statement;

@Override
public <R, C> R accept(AbstractNodeVisitor<R, C> visitor, C context) {
return visitor.visitExplain(this, context);
}
}
35 changes: 35 additions & 0 deletions core/src/main/java/org/opensearch/sql/ast/statement/Query.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.sql.ast.statement;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.opensearch.sql.ast.AbstractNodeVisitor;
import org.opensearch.sql.ast.tree.UnresolvedPlan;

/**
* Query Statement.
*/
@Getter
@Setter
@ToString
@EqualsAndHashCode(callSuper = false)
@RequiredArgsConstructor
public class Query extends Statement {

private final UnresolvedPlan plan;

@Override
public <R, C> R accept(AbstractNodeVisitor<R, C> visitor, C context) {
return visitor.visitQuery(this, context);
}
}
22 changes: 22 additions & 0 deletions core/src/main/java/org/opensearch/sql/ast/statement/Statement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.sql.ast.statement;

import org.opensearch.sql.ast.AbstractNodeVisitor;
import org.opensearch.sql.ast.Node;

/**
* Statement is the high interface of core engine.
*/
public abstract class Statement extends Node {
@Override
public <R, C> R accept(AbstractNodeVisitor<R, C> visitor, C context) {
return visitor.visitStatement(this, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.sql.executor;

import org.opensearch.sql.executor.execution.AbstractPlan;

/**
* Default QueryManager implementation which execute {@link AbstractPlan} on caller thread.
*/
public class DefaultQueryManager implements QueryManager {

@Override
public QueryId submit(AbstractPlan queryExecution) {
queryExecution.execute();

return queryExecution.getQueryId();
}
}
36 changes: 36 additions & 0 deletions core/src/main/java/org/opensearch/sql/executor/QueryId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.sql.executor;

import lombok.Getter;
import org.apache.commons.lang3.RandomStringUtils;
import org.opensearch.sql.executor.execution.AbstractPlan;

/**
* Query id of {@link AbstractPlan}.
*/
public class QueryId {
/**
* Query id.
*/
@Getter
private final String queryId;

/**
* Generate {@link QueryId}.
* @return {@link QueryId}.
*/
public static QueryId queryId() {
return new QueryId(RandomStringUtils.random(10, true, true));
}

private QueryId(String queryId) {
this.queryId = queryId;
}
}
25 changes: 25 additions & 0 deletions core/src/main/java/org/opensearch/sql/executor/QueryManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.sql.executor;

import org.opensearch.sql.executor.execution.AbstractPlan;

/**
* QueryManager is the high-level interface of core engine.
* Frontend submit {@link AbstractPlan} to QueryManager.
*/
public interface QueryManager {

/**
* Submit {@link AbstractPlan}.
* @param queryPlan {@link AbstractPlan}.
* @return {@link QueryId}.
*/
QueryId submit(AbstractPlan queryPlan);
}
70 changes: 70 additions & 0 deletions core/src/main/java/org/opensearch/sql/executor/QueryService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.sql.executor;

import lombok.RequiredArgsConstructor;
import org.opensearch.sql.analysis.AnalysisContext;
import org.opensearch.sql.analysis.Analyzer;
import org.opensearch.sql.ast.tree.UnresolvedPlan;
import org.opensearch.sql.common.response.ResponseListener;
import org.opensearch.sql.planner.Planner;
import org.opensearch.sql.planner.logical.LogicalPlan;
import org.opensearch.sql.planner.physical.PhysicalPlan;

/**
* The low level interface of core engine.
*/
@RequiredArgsConstructor
public class QueryService {

private final Analyzer analyzer;

private final ExecutionEngine executionEngine;

private final Planner planner;

/**
* Execute the {@link UnresolvedPlan}, using {@link ResponseListener} to get response.
*
* @param plan {@link UnresolvedPlan}
* @param listener {@link ResponseListener}
*/
public void execute(UnresolvedPlan plan,
ResponseListener<ExecutionEngine.QueryResponse> listener) {
try {
executionEngine.execute(plan(plan), listener);
} catch (Exception e) {
listener.onFailure(e);
}
}

/**
* Explain the query in {@link UnresolvedPlan} using {@link ResponseListener} to
* get and format explain response.
*
* @param plan {@link UnresolvedPlan}
* @param listener {@link ResponseListener} for explain response
*/
public void explain(UnresolvedPlan plan,
ResponseListener<ExecutionEngine.ExplainResponse> listener) {
try {
executionEngine.explain(plan(plan), listener);
} catch (Exception e) {
listener.onFailure(e);
}
}

private PhysicalPlan plan(UnresolvedPlan plan) {
// 1.Analyze abstract syntax to generate logical plan
LogicalPlan logicalPlan = analyzer.analyze(plan, new AnalysisContext());

// 2.Generate optimal physical plan from logical plan
return planner.plan(logicalPlan);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.sql.executor.execution;


import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.opensearch.sql.common.response.ResponseListener;
import org.opensearch.sql.executor.ExecutionEngine;
import org.opensearch.sql.executor.QueryId;

/**
* AbstractPlan represent the execution entity of the Statement.
*/
@RequiredArgsConstructor
public abstract class AbstractPlan {

/**
* Uniq query id.
*/
@Getter
private final QueryId queryId;

/**
* Start query execution.
*/
public abstract void execute();

/**
* Explain query execution.
*
* @param listener query explain response listener.
*/
public abstract void explain(ResponseListener<ExecutionEngine.ExplainResponse> listener);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.sql.executor.execution;

import org.opensearch.sql.common.response.ResponseListener;
import org.opensearch.sql.executor.ExecutionEngine;
import org.opensearch.sql.executor.QueryId;

/**
* Explain plan.
*/
public class ExplainPlan extends AbstractPlan {

private final AbstractPlan plan;

private final ResponseListener<ExecutionEngine.ExplainResponse> explainListener;

/**
* Constructor.
*/
public ExplainPlan(QueryId queryId,
AbstractPlan plan,
ResponseListener<ExecutionEngine.ExplainResponse> explainListener) {
super(queryId);
this.plan = plan;
this.explainListener = explainListener;
}

@Override
public void execute() {
plan.explain(explainListener);
}

@Override
public void explain(ResponseListener<ExecutionEngine.ExplainResponse> listener) {
throw new UnsupportedOperationException("explain query can not been explained.");
}
}
Loading

0 comments on commit 30fcd79

Please sign in to comment.