Skip to content
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

PoC: Fix aliases in WHERE clause #115

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
package org.opensearch.sql.analysis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Getter;
import org.opensearch.sql.expression.NamedExpression;
Expand All @@ -22,6 +24,8 @@ public class AnalysisContext {
private TypeEnvironment environment;
@Getter
private final List<NamedExpression> namedParseExpressions;
@Getter
private final Map<String, String> aliases;

public AnalysisContext() {
this(new TypeEnvironment(null));
Expand All @@ -30,6 +34,7 @@ public AnalysisContext() {
public AnalysisContext(TypeEnvironment environment) {
this.environment = environment;
this.namedParseExpressions = new ArrayList<>();
this.aliases = new HashMap<>();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/org/opensearch/sql/analysis/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.opensearch.sql.ast.tree.Sort.NullOrder.NULL_LAST;
import static org.opensearch.sql.ast.tree.Sort.SortOrder.ASC;
import static org.opensearch.sql.ast.tree.Sort.SortOrder.DESC;
import static org.opensearch.sql.data.type.ExprCoreType.STRING;
import static org.opensearch.sql.data.type.ExprCoreType.STRUCT;
import static org.opensearch.sql.utils.MLCommonsConstants.RCF_ANOMALOUS;
import static org.opensearch.sql.utils.MLCommonsConstants.RCF_ANOMALY_GRADE;
Expand All @@ -31,6 +32,7 @@
import org.opensearch.sql.analysis.symbol.Namespace;
import org.opensearch.sql.analysis.symbol.Symbol;
import org.opensearch.sql.ast.AbstractNodeVisitor;
import org.opensearch.sql.ast.expression.Alias;
import org.opensearch.sql.ast.expression.Argument;
import org.opensearch.sql.ast.expression.Field;
import org.opensearch.sql.ast.expression.Let;
Expand Down Expand Up @@ -268,6 +270,14 @@ public LogicalPlan visitRareTopN(RareTopN node, AnalysisContext context) {
*/
@Override
public LogicalPlan visitProject(Project node, AnalysisContext context) {
node.getProjectList().forEach(proj -> {
if (proj instanceof Alias) {
context.getAliases().put(((Alias) proj).getAlias(), ((Alias) proj).getName());
//context.peek().define(new Symbol(Namespace.FIELD_NAME, ((Alias)proj).getAlias()), STRING);
//new ReferenceExpression()
//
}
});
LogicalPlan child = node.getChild().get(0).accept(this, context);

if (node.hasArgument()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ private Expression visitIdentifier(String ident, AnalysisContext context) {
}
}

ident = context.getAliases().getOrDefault(ident, ident);

TypeEnvironment typeEnv = context.peek();
ReferenceExpression ref = DSL.ref(ident,
typeEnv.resolve(new Symbol(Namespace.FIELD_NAME, ident)));
Expand Down