forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding nested function support in SELECT clause.
Signed-off-by: forestmvey <forestv@bitquilltech.com>
- Loading branch information
1 parent
23cc0f6
commit c02fa2a
Showing
66 changed files
with
2,180 additions
and
107 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
70 changes: 70 additions & 0 deletions
70
core/src/main/java/org/opensearch/sql/analysis/NestedAnalyzer.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,70 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.analysis; | ||
|
||
import static org.opensearch.sql.data.type.ExprCoreType.STRING; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
import org.opensearch.sql.ast.expression.Alias; | ||
import org.opensearch.sql.ast.expression.Function; | ||
import org.opensearch.sql.ast.expression.UnresolvedExpression; | ||
import org.opensearch.sql.expression.NamedExpression; | ||
import org.opensearch.sql.expression.ReferenceExpression; | ||
import org.opensearch.sql.expression.function.BuiltinFunctionName; | ||
import org.opensearch.sql.planner.logical.LogicalNested; | ||
import org.opensearch.sql.planner.logical.LogicalPlan; | ||
|
||
/** | ||
* Analyze the Nested Function in the {@link AnalysisContext} to construct the {@link | ||
* LogicalPlan}. | ||
*/ | ||
@RequiredArgsConstructor | ||
public class NestedAnalyzer extends AbstractNodeVisitor<LogicalPlan, AnalysisContext> { | ||
private final List<NamedExpression> namedExpressions; | ||
private final ExpressionAnalyzer expressionAnalyzer; | ||
private final LogicalPlan child; | ||
|
||
public LogicalPlan analyze(UnresolvedExpression projectItem, AnalysisContext context) { | ||
LogicalPlan nested = projectItem.accept(this, context); | ||
return (nested == null) ? child : nested; | ||
} | ||
|
||
@Override | ||
public LogicalPlan visitAlias(Alias node, AnalysisContext context) { | ||
return node.getDelegated().accept(this, context); | ||
} | ||
|
||
@Override | ||
public LogicalPlan visitFunction(Function node, AnalysisContext context) { | ||
if (node.getFuncName().equalsIgnoreCase(BuiltinFunctionName.NESTED.name())) { | ||
|
||
List<UnresolvedExpression> expressions = node.getFuncArgs(); | ||
ReferenceExpression nestedField = | ||
(ReferenceExpression)expressionAnalyzer.analyze(expressions.get(0), context); | ||
Map<String, ReferenceExpression> args; | ||
if (expressions.size() == 2) { | ||
args = Map.of( | ||
"field", nestedField, | ||
"path", (ReferenceExpression)expressionAnalyzer.analyze(expressions.get(1), context) | ||
); | ||
} else { | ||
args = Map.of( | ||
"field", (ReferenceExpression)expressionAnalyzer.analyze(expressions.get(0), context), | ||
"path", generatePath(nestedField.toString()) | ||
); | ||
} | ||
return new LogicalNested(child, List.of(args), namedExpressions); | ||
} | ||
return null; | ||
} | ||
|
||
private ReferenceExpression generatePath(String field) { | ||
return new ReferenceExpression(field.substring(0, field.lastIndexOf(".")), STRING); | ||
} | ||
} |
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
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
32 changes: 32 additions & 0 deletions
32
core/src/main/java/org/opensearch/sql/expression/function/NestedFunctionResolver.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,32 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.expression.function; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.opensearch.sql.exception.SemanticCheckException; | ||
import org.opensearch.sql.expression.nested.NestedFunction; | ||
|
||
@RequiredArgsConstructor | ||
public class NestedFunctionResolver | ||
implements FunctionResolver { | ||
|
||
@Getter | ||
private final FunctionName functionName; | ||
|
||
@Override | ||
public Pair<FunctionSignature, FunctionBuilder> resolve(FunctionSignature unresolvedSignature) { | ||
if (!unresolvedSignature.getFunctionName().equals(functionName)) { | ||
throw new SemanticCheckException(String.format("Expected '%s' but got '%s'", | ||
functionName.getFunctionName(), unresolvedSignature.getFunctionName().getFunctionName())); | ||
} | ||
|
||
FunctionBuilder buildFunction = (functionProperties, args) | ||
-> new NestedFunction(functionName, args); | ||
return Pair.of(unresolvedSignature, buildFunction); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
core/src/main/java/org/opensearch/sql/expression/nested/NestedFunction.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,38 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.expression.nested; | ||
|
||
import java.util.List; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.env.Environment; | ||
import org.opensearch.sql.expression.function.FunctionName; | ||
import org.opensearch.sql.expression.function.OpenSearchFunctions; | ||
|
||
public class NestedFunction extends OpenSearchFunctions.OpenSearchFunction { | ||
private final List<Expression> arguments; | ||
|
||
/** | ||
* Required argument constructor. | ||
* @param functionName name of the function | ||
* @param arguments a list of expressions | ||
*/ | ||
public NestedFunction(FunctionName functionName, List<Expression> arguments) { | ||
super(functionName, arguments); | ||
this.arguments = arguments; | ||
} | ||
|
||
@Override | ||
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | ||
return valueEnv.resolve(this.arguments.get(0)); | ||
} | ||
|
||
@Override | ||
public ExprType type() { | ||
return this.arguments.get(0).type(); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
core/src/main/java/org/opensearch/sql/planner/logical/LogicalNested.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,42 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.planner.logical; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import org.opensearch.sql.expression.NamedExpression; | ||
import org.opensearch.sql.expression.ReferenceExpression; | ||
|
||
@EqualsAndHashCode(callSuper = true) | ||
@Getter | ||
@ToString | ||
public class LogicalNested extends LogicalPlan { | ||
private final List<Map<String, ReferenceExpression>> fields; | ||
private final List<NamedExpression> projectList; | ||
|
||
/** | ||
* Constructor of LogicalNested. | ||
* | ||
*/ | ||
public LogicalNested( | ||
LogicalPlan childPlan, | ||
List<Map<String, ReferenceExpression>> fields, | ||
List<NamedExpression> projectList | ||
) { | ||
super(Collections.singletonList(childPlan)); | ||
this.fields = fields; | ||
this.projectList = projectList; | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(LogicalPlanNodeVisitor<R, C> visitor, C context) { | ||
return visitor.visitUnnest(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
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.