This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support CASE clause in new engine (#818)
* Change grammar and UT * Add case expression * Pass jacoco in sql module * Pass jacoco in core module and refactor when clause * Fix broken IT and add javadoc * Add comparison test for basic case * Add comparison test for complex case * Add doctest * Add type check * Prepare PR * Prepare PR * Address PR comments
- Loading branch information
Showing
21 changed files
with
833 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/expression/Case.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.sql.ast.expression; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor; | ||
import com.amazon.opendistroforelasticsearch.sql.ast.Node; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
/** | ||
* AST node that represents CASE clause similar as Switch statement in programming language. | ||
*/ | ||
@AllArgsConstructor | ||
@EqualsAndHashCode(callSuper = false) | ||
@Getter | ||
@ToString | ||
public class Case extends UnresolvedExpression { | ||
|
||
/** | ||
* Value to be compared by WHEN statements. Null in the case of CASE WHEN conditions. | ||
*/ | ||
private final UnresolvedExpression caseValue; | ||
|
||
/** | ||
* Expression list that represents WHEN statements. Each is a mapping from condition | ||
* to its result. | ||
*/ | ||
private final List<When> whenClauses; | ||
|
||
/** | ||
* Expression that represents ELSE statement result. | ||
*/ | ||
private final UnresolvedExpression elseClause; | ||
|
||
@Override | ||
public List<? extends Node> getChild() { | ||
ImmutableList.Builder<Node> children = ImmutableList.builder(); | ||
if (caseValue != null) { | ||
children.add(caseValue); | ||
} | ||
children.addAll(whenClauses); | ||
|
||
if (elseClause != null) { | ||
children.add(elseClause); | ||
} | ||
return children.build(); | ||
} | ||
|
||
@Override | ||
public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) { | ||
return nodeVisitor.visitCase(this, context); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/expression/When.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.sql.ast.expression; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.ast.AbstractNodeVisitor; | ||
import com.amazon.opendistroforelasticsearch.sql.ast.Node; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.List; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.ToString; | ||
|
||
/** | ||
* AST node that represents WHEN clause. | ||
*/ | ||
@EqualsAndHashCode(callSuper = false) | ||
@Getter | ||
@RequiredArgsConstructor | ||
@ToString | ||
public class When extends UnresolvedExpression { | ||
|
||
/** | ||
* WHEN condition, either a search condition or compare value if case value present. | ||
*/ | ||
private final UnresolvedExpression condition; | ||
|
||
/** | ||
* Result to return if condition matched. | ||
*/ | ||
private final UnresolvedExpression result; | ||
|
||
@Override | ||
public List<? extends Node> getChild() { | ||
return ImmutableList.of(condition, result); | ||
} | ||
|
||
@Override | ||
public <T, C> T accept(AbstractNodeVisitor<T, C> nodeVisitor, C context) { | ||
return nodeVisitor.visitWhen(this, context); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.