forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Nested Function Support in SELECT Clause #243
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
Yury-Fridlyand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return null; | ||
Yury-Fridlyand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
/* | ||
* 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) { | ||
// Only need to resolve field argument which is always first index in member variable. | ||
return valueEnv.resolve(this.arguments.get(0)); | ||
forestmvey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You overwrite
child
set byhighlightAnalyzer
, which overwriteschild
set bywindowAnalyzer
, which .... Is it safe? Isnested
compatible withhighlight
?Do you have [IT] tests for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't believe we have any requirement for
nested
to work with added V2 functionality likehighlight
. Goal is legacy deprecation and we want to abandon the NLPChina implementation for a better standard like PartiQL. I can pushDownhighlight
andnested
, I'm not sure what a valid query would be though?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
select nested(...), highlight(...) from ... where match(...)
Can you try this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not working with Relevance Queries. Issue here for tracking.
1488