Skip to content

Commit

Permalink
add CalciteExecutionEngine
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Huo <penghuo@gmail.com>
  • Loading branch information
penghuo committed Oct 19, 2024
1 parent 98dbabb commit 52cbf9b
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 15 deletions.
9 changes: 9 additions & 0 deletions async-query-core/src/main/antlr/SqlBaseLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ BUCKETS: 'BUCKETS';
BY: 'BY';
BYTE: 'BYTE';
CACHE: 'CACHE';
CALL: 'CALL';
CALLED: 'CALLED';
CASCADE: 'CASCADE';
CASE: 'CASE';
Expand Down Expand Up @@ -255,12 +256,14 @@ BINARY_HEX: 'X';
HOUR: 'HOUR';
HOURS: 'HOURS';
IDENTIFIER_KW: 'IDENTIFIER';
IDENTITY: 'IDENTITY';
IF: 'IF';
IGNORE: 'IGNORE';
IMMEDIATE: 'IMMEDIATE';
IMPORT: 'IMPORT';
IN: 'IN';
INCLUDE: 'INCLUDE';
INCREMENT: 'INCREMENT';
INDEX: 'INDEX';
INDEXES: 'INDEXES';
INNER: 'INNER';
Expand All @@ -276,13 +279,15 @@ INTO: 'INTO';
INVOKER: 'INVOKER';
IS: 'IS';
ITEMS: 'ITEMS';
ITERATE: 'ITERATE';
JOIN: 'JOIN';
KEYS: 'KEYS';
LANGUAGE: 'LANGUAGE';
LAST: 'LAST';
LATERAL: 'LATERAL';
LAZY: 'LAZY';
LEADING: 'LEADING';
LEAVE: 'LEAVE';
LEFT: 'LEFT';
LIKE: 'LIKE';
ILIKE: 'ILIKE';
Expand All @@ -296,6 +301,7 @@ LOCK: 'LOCK';
LOCKS: 'LOCKS';
LOGICAL: 'LOGICAL';
LONG: 'LONG';
LOOP: 'LOOP';
MACRO: 'MACRO';
MAP: 'MAP' {incComplexTypeLevelCounter();};
MATCHED: 'MATCHED';
Expand Down Expand Up @@ -362,6 +368,7 @@ REFERENCES: 'REFERENCES';
REFRESH: 'REFRESH';
RENAME: 'RENAME';
REPAIR: 'REPAIR';
REPEAT: 'REPEAT';
REPEATABLE: 'REPEATABLE';
REPLACE: 'REPLACE';
RESET: 'RESET';
Expand Down Expand Up @@ -451,6 +458,7 @@ UNKNOWN: 'UNKNOWN';
UNLOCK: 'UNLOCK';
UNPIVOT: 'UNPIVOT';
UNSET: 'UNSET';
UNTIL: 'UNTIL';
UPDATE: 'UPDATE';
USE: 'USE';
USER: 'USER';
Expand Down Expand Up @@ -501,6 +509,7 @@ TILDE: '~';
AMPERSAND: '&';
PIPE: '|';
CONCAT_PIPE: '||';
OPERATOR_PIPE: '|>';
HAT: '^';
COLON: ':';
DOUBLE_COLON: '::';
Expand Down
87 changes: 85 additions & 2 deletions async-query-core/src/main/antlr/SqlBaseParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ compoundStatement
| setStatementWithOptionalVarKeyword
| beginEndCompoundBlock
| ifElseStatement
| caseStatement
| whileStatement
| repeatStatement
| leaveStatement
| iterateStatement
| loopStatement
;

setStatementWithOptionalVarKeyword
Expand All @@ -83,6 +88,29 @@ ifElseStatement
(ELSE elseBody=compoundBody)? END IF
;

repeatStatement
: beginLabel? REPEAT compoundBody UNTIL booleanExpression END REPEAT endLabel?
;

leaveStatement
: LEAVE multipartIdentifier
;

iterateStatement
: ITERATE multipartIdentifier
;

caseStatement
: CASE (WHEN conditions+=booleanExpression THEN conditionalBodies+=compoundBody)+
(ELSE elseBody=compoundBody)? END CASE #searchedCaseStatement
| CASE caseVariable=expression (WHEN conditionExpressions+=expression THEN conditionalBodies+=compoundBody)+
(ELSE elseBody=compoundBody)? END CASE #simpleCaseStatement
;

loopStatement
: beginLabel? LOOP compoundBody END LOOP endLabel?
;

singleStatement
: (statement|setResetStatement) SEMICOLON* EOF
;
Expand Down Expand Up @@ -125,7 +153,7 @@ statement
| ctes? dmlStatementNoWith #dmlStatement
| USE identifierReference #use
| USE namespace identifierReference #useNamespace
| SET CATALOG (errorCapturingIdentifier | stringLit) #setCatalog
| SET CATALOG catalogIdentifierReference #setCatalog
| CREATE namespace (IF errorCapturingNot EXISTS)? identifierReference
(commentSpec |
locationSpec |
Expand Down Expand Up @@ -275,6 +303,10 @@ statement
LEFT_PAREN columns=multipartIdentifierPropertyList RIGHT_PAREN
(OPTIONS options=propertyList)? #createIndex
| DROP INDEX (IF EXISTS)? identifier ON TABLE? identifierReference #dropIndex
| CALL identifierReference
LEFT_PAREN
(functionArgument (COMMA functionArgument)*)?
RIGHT_PAREN #call
| unsupportedHiveNativeCommands .*? #failNativeCommand
;

Expand Down Expand Up @@ -567,6 +599,12 @@ identifierReference
| multipartIdentifier
;

catalogIdentifierReference
: IDENTIFIER_KW LEFT_PAREN expression RIGHT_PAREN
| errorCapturingIdentifier
| stringLit
;

queryOrganization
: (ORDER BY order+=sortItem (COMMA order+=sortItem)*)?
(CLUSTER BY clusterBy+=expression (COMMA clusterBy+=expression)*)?
Expand All @@ -589,6 +627,7 @@ queryTerm
operator=INTERSECT setQuantifier? right=queryTerm #setOperation
| left=queryTerm {!legacy_setops_precedence_enabled}?
operator=(UNION | EXCEPT | SETMINUS) setQuantifier? right=queryTerm #setOperation
| left=queryTerm OPERATOR_PIPE operatorPipeRightSide #operatorPipeStatement
;

queryPrimary
Expand Down Expand Up @@ -1272,7 +1311,22 @@ colDefinitionOption
;

generationExpression
: GENERATED ALWAYS AS LEFT_PAREN expression RIGHT_PAREN
: GENERATED ALWAYS AS LEFT_PAREN expression RIGHT_PAREN #generatedColumn
| GENERATED (ALWAYS | BY DEFAULT) AS IDENTITY identityColSpec? #identityColumn
;

identityColSpec
: LEFT_PAREN sequenceGeneratorOption* RIGHT_PAREN
;

sequenceGeneratorOption
: START WITH start=sequenceGeneratorStartOrStep
| INCREMENT BY step=sequenceGeneratorStartOrStep
;

sequenceGeneratorStartOrStep
: MINUS? INTEGER_VALUE
| MINUS? BIGINT_LITERAL
;

complexColTypeList
Expand Down Expand Up @@ -1447,6 +1501,20 @@ version
| stringLit
;

operatorPipeRightSide
: selectClause
| whereClause
// The following two cases match the PIVOT or UNPIVOT clause, respectively.
// For each one, we add the other clause as an option in order to return high-quality error
// messages in the event that both are present (this is not allowed).
| pivotClause unpivotClause?
| unpivotClause pivotClause?
| sample
| joinRelation
| operator=(UNION | EXCEPT | SETMINUS | INTERSECT) setQuantifier? right=queryTerm
| queryOrganization
;

// When `SQL_standard_keyword_behavior=true`, there are 2 kinds of keywords in Spark SQL.
// - Reserved keywords:
// Keywords that are reserved and can't be used as identifiers for table, view, column,
Expand Down Expand Up @@ -1562,11 +1630,13 @@ ansiNonReserved
| HOUR
| HOURS
| IDENTIFIER_KW
| IDENTITY
| IF
| IGNORE
| IMMEDIATE
| IMPORT
| INCLUDE
| INCREMENT
| INDEX
| INDEXES
| INPATH
Expand All @@ -1578,10 +1648,12 @@ ansiNonReserved
| INTERVAL
| INVOKER
| ITEMS
| ITERATE
| KEYS
| LANGUAGE
| LAST
| LAZY
| LEAVE
| LIKE
| ILIKE
| LIMIT
Expand All @@ -1594,6 +1666,7 @@ ansiNonReserved
| LOCKS
| LOGICAL
| LONG
| LOOP
| MACRO
| MAP
| MATCHED
Expand Down Expand Up @@ -1648,6 +1721,7 @@ ansiNonReserved
| REFRESH
| RENAME
| REPAIR
| REPEAT
| REPEATABLE
| REPLACE
| RESET
Expand Down Expand Up @@ -1723,6 +1797,7 @@ ansiNonReserved
| UNLOCK
| UNPIVOT
| UNSET
| UNTIL
| UPDATE
| USE
| VALUES
Expand Down Expand Up @@ -1802,6 +1877,7 @@ nonReserved
| BY
| BYTE
| CACHE
| CALL
| CALLED
| CASCADE
| CASE
Expand Down Expand Up @@ -1908,12 +1984,14 @@ nonReserved
| HOUR
| HOURS
| IDENTIFIER_KW
| IDENTITY
| IF
| IGNORE
| IMMEDIATE
| IMPORT
| IN
| INCLUDE
| INCREMENT
| INDEX
| INDEXES
| INPATH
Expand All @@ -1927,11 +2005,13 @@ nonReserved
| INVOKER
| IS
| ITEMS
| ITERATE
| KEYS
| LANGUAGE
| LAST
| LAZY
| LEADING
| LEAVE
| LIKE
| LONG
| ILIKE
Expand All @@ -1945,6 +2025,7 @@ nonReserved
| LOCKS
| LOGICAL
| LONG
| LOOP
| MACRO
| MAP
| MATCHED
Expand Down Expand Up @@ -2009,6 +2090,7 @@ nonReserved
| REFRESH
| RENAME
| REPAIR
| REPEAT
| REPEATABLE
| REPLACE
| RESET
Expand Down Expand Up @@ -2093,6 +2175,7 @@ nonReserved
| UNLOCK
| UNPIVOT
| UNSET
| UNTIL
| UPDATE
| USE
| USER
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ allprojects {
resolutionStrategy.force 'org.apache.commons:commons-text:1.11.0'
resolutionStrategy.force 'commons-io:commons-io:2.15.0'
resolutionStrategy.force 'org.yaml:snakeyaml:2.2'

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.opensearch.sql.ast.tree.UnresolvedPlan;
import org.opensearch.sql.common.response.ResponseListener;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.data.type.ExprType;
Expand All @@ -32,6 +33,13 @@ public interface ExecutionEngine {
void execute(
PhysicalPlan plan, ExecutionContext context, ResponseListener<QueryResponse> listener);


// FIXME
default void execute(UnresolvedPlan plan, ExecutionContext context,
ResponseListener<QueryResponse> listener) {
throw new UnsupportedOperationException();
}

/**
* Explain physical plan and call back response listener. The reason why this has to be part of
* execution engine interface is that the physical plan probably needs to be executed to get more
Expand Down
18 changes: 17 additions & 1 deletion core/src/main/java/org/opensearch/sql/executor/QueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class QueryService {
public void execute(
UnresolvedPlan plan, ResponseListener<ExecutionEngine.QueryResponse> listener) {
try {
executePlan(analyze(plan), PlanContext.emptyPlanContext(), listener);
executePlanInternal(plan, PlanContext.emptyPlanContext(), listener);
} catch (Exception e) {
listener.onFailure(e);
}
Expand All @@ -53,6 +53,22 @@ public void execute(
* @param planContext {@link PlanContext}
* @param listener {@link ResponseListener}
*/
public void executePlanInternal(
UnresolvedPlan plan,
PlanContext planContext,
ResponseListener<ExecutionEngine.QueryResponse> listener) {
try {
planContext
.getSplit()
.ifPresentOrElse(
split -> executionEngine.execute(plan, new ExecutionContext(split), listener),
() ->
executionEngine.execute(plan, ExecutionContext.emptyExecutionContext(), listener));
} catch (Exception e) {
listener.onFailure(e);
}
}

public void executePlan(
LogicalPlan plan,
PlanContext planContext,
Expand Down
12 changes: 12 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

./gradlew assemble

OS_RELEASE="opensearch-2.17.1"
SQL_PLUGIN="opensearch-sql-2.17.1.0"
SQL_PLUGIN_ZIP="${SQL_PLUGIN}-SNAPSHOT.zip"

rm -rf /Users/penghuo/release/opensearch/${OS_RELEASE}/plugins/${SQL_PLUGIN}
cp /Users/penghuo/oss/os-sql/plugin/build/distributions/${SQL_PLUGIN_ZIP} /Users/penghuo/release/opensearch/${OS_RELEASE}/plugins
unzip /Users/penghuo/release/opensearch/${OS_RELEASE}/plugins/${SQL_PLUGIN_ZIP} -d /Users/penghuo/release/opensearch/${OS_RELEASE}/plugins/${SQL_PLUGIN}
rm /Users/penghuo/release/opensearch/${OS_RELEASE}/plugins/${SQL_PLUGIN_ZIP}
2 changes: 1 addition & 1 deletion opensearch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {

// Apache Calcite
implementation 'org.apache.calcite:calcite-core:1.38.0'
implementation 'org.apache.calcite:calcite-linq4j:1.38.0'
compileOnly "org.immutables:value-annotations:2.8.8"

testImplementation('org.junit.jupiter:junit-jupiter-api:5.9.3')
testImplementation('org.junit.jupiter:junit-jupiter-params:5.9.3')
Expand Down
Loading

0 comments on commit 52cbf9b

Please sign in to comment.