-
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
[DRAFT] Array Support POC #282
base: integ-array-support-poc
Are you sure you want to change the base?
Changes from all commits
1e3d879
05048cc
95e2c82
579d44a
41d7bf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,11 @@ | |
import org.opensearch.sql.ast.expression.NestedAllTupleFields; | ||
import org.opensearch.sql.ast.expression.QualifiedName; | ||
import org.opensearch.sql.ast.expression.UnresolvedExpression; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.expression.DSL; | ||
import org.opensearch.sql.expression.Expression; | ||
import org.opensearch.sql.expression.ArrayReferenceExpression; | ||
import org.opensearch.sql.expression.NamedExpression; | ||
import org.opensearch.sql.expression.ReferenceExpression; | ||
|
||
|
@@ -105,8 +107,17 @@ public List<NamedExpression> visitAllFields(AllFields node, | |
AnalysisContext context) { | ||
TypeEnvironment environment = context.peek(); | ||
Map<String, ExprType> lookupAllFields = environment.lookupAllFields(Namespace.FIELD_NAME); | ||
return lookupAllFields.entrySet().stream().map(entry -> DSL.named(entry.getKey(), | ||
new ReferenceExpression(entry.getKey(), entry.getValue()))).collect(Collectors.toList()); | ||
return lookupAllFields.entrySet().stream().map(entry -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add an example in the PR description for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes the functionality for |
||
{ | ||
ReferenceExpression ref = new ReferenceExpression(entry.getKey(), entry.getValue()); | ||
if (entry.getValue().equals(ExprCoreType.ARRAY)) { | ||
return DSL.named(entry.getKey(), | ||
new ArrayReferenceExpression(ref)); | ||
} else { | ||
return DSL.named(entry.getKey(), ref); | ||
} | ||
} | ||
).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
package org.opensearch.sql.ast.expression; | ||
|
||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.opensearch.sql.ast.AbstractNodeVisitor; | ||
|
||
import java.util.List; | ||
import java.util.OptionalInt; | ||
import java.util.stream.Collectors; | ||
|
||
@Getter | ||
@EqualsAndHashCode(callSuper = false) | ||
public class ArrayQualifiedName extends QualifiedName { | ||
|
||
private final List<Pair<String, OptionalInt>> partsAndIndexes; | ||
|
||
public ArrayQualifiedName(List<Pair<String, OptionalInt>> parts) { | ||
super(parts.stream().map(p -> p.getLeft()).collect(Collectors.toList())); | ||
this.partsAndIndexes = parts; | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(AbstractNodeVisitor<R, C> nodeVisitor, C context) { | ||
return nodeVisitor.visitArrayQualifiedName(this, context); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
package org.opensearch.sql.expression; | ||
|
||
import static org.opensearch.sql.utils.ExpressionUtils.PATH_SEP; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.OptionalInt; | ||
import java.util.stream.Collectors; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.opensearch.sql.common.utils.StringUtils; | ||
import org.opensearch.sql.data.model.ExprCollectionValue; | ||
import org.opensearch.sql.data.model.ExprMissingValue; | ||
import org.opensearch.sql.data.model.ExprTupleValue; | ||
import org.opensearch.sql.data.model.ExprValue; | ||
import org.opensearch.sql.data.model.ExprValueUtils; | ||
import org.opensearch.sql.data.type.ExprCoreType; | ||
import org.opensearch.sql.data.type.ExprType; | ||
import org.opensearch.sql.expression.env.Environment; | ||
|
||
@EqualsAndHashCode | ||
public class ArrayReferenceExpression extends ReferenceExpression { | ||
@Getter | ||
private final List<Pair<String, OptionalInt>> partsAndIndexes; | ||
@Getter | ||
private final ExprType type; | ||
public ArrayReferenceExpression(String ref, ExprType type, List<Pair<String, OptionalInt>> partsAndIndexes) { | ||
super(StringUtils.removeParenthesis(ref), type); | ||
this.partsAndIndexes = partsAndIndexes; | ||
this.type = type; | ||
} | ||
|
||
public ArrayReferenceExpression(ReferenceExpression ref) { | ||
super(StringUtils.removeParenthesis(ref.toString()), ref.type()); | ||
this.partsAndIndexes = Arrays.stream(ref.toString().split("\\.")).map(e -> Pair.of(e, OptionalInt.empty())).collect( | ||
Collectors.toList()); | ||
this.type = ref.type(); | ||
} | ||
|
||
@Override | ||
public ExprValue valueOf(Environment<Expression, ExprValue> env) { | ||
return env.resolve(this); | ||
} | ||
|
||
@Override | ||
public ExprType type() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public <T, C> T accept(ExpressionNodeVisitor<T, C> visitor, C context) { | ||
return visitor.visitArrayReference(this, context); | ||
} | ||
|
||
public ExprValue resolve(ExprTupleValue value) { | ||
return resolve(value, partsAndIndexes); | ||
} | ||
|
||
private ExprValue resolve(ExprValue value, List<Pair<String, OptionalInt>> paths) { | ||
List<String> pathsWithoutParenthesis = | ||
paths.stream().map(p -> StringUtils.removeParenthesis(p.getLeft())).collect(Collectors.toList()); | ||
ExprValue wholePathValue = value.keyValue(String.join(PATH_SEP, pathsWithoutParenthesis)); | ||
|
||
if (!paths.get(0).getRight().isEmpty()) { | ||
if (value.keyValue(pathsWithoutParenthesis.get(0)) instanceof ExprCollectionValue) { // TODO check array size | ||
wholePathValue = value | ||
.keyValue(pathsWithoutParenthesis.get(0)) | ||
.collectionValue() | ||
.get(paths.get(0).getRight().getAsInt()); | ||
if (paths.size() != 1) { | ||
return resolve(wholePathValue, paths.subList(1, paths.size())); | ||
} | ||
} else { | ||
return ExprValueUtils.missingValue(); | ||
} | ||
} else if (wholePathValue.isMissing()) { | ||
return resolve(value.keyValue(pathsWithoutParenthesis.get(0)), paths.subList(1, paths.size())); | ||
} | ||
|
||
if (!wholePathValue.isMissing() || paths.size() == 1) { | ||
return wholePathValue; | ||
} else { | ||
return resolve(value.keyValue(pathsWithoutParenthesis.get(0)), paths.subList(1, paths.size())); | ||
} | ||
} | ||
} |
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.
Is it a regex? Do you want to match
:
or\d+:\d+
(or even\d*:\d*
)?Add a comment for this function please.