Skip to content

Commit

Permalink
(#1506) Remove reservedSymbolTable and replace with HIDDEN_FIELD_NAME (
Browse files Browse the repository at this point in the history
…#1936) (#1964)

* (#1506) Remove reservedSymbolTable and replace with HIDDEN_FIELD_NAME (#323)

* #1506: Remove reservedSymbolTable and use HIDDEN_FIELD_NAME instead



* #1506: Remove reservedSymbolTable and use HIDDEN_FIELD_NAME instead



* #1506: Fix checkstyle errors



---------



* #1506: spotless apply



---------


(cherry picked from commit 5381a6f)

Signed-off-by: acarbonetto <[email protected]>
  • Loading branch information
acarbonetto authored Aug 21, 2023
1 parent d9a78df commit 0b419ff
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 25 deletions.
21 changes: 12 additions & 9 deletions core/src/main/java/org/opensearch/sql/analysis/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ public LogicalPlan visitRelation(Relation node, AnalysisContext context) {
dataSourceSchemaIdentifierNameResolver.getIdentifierName());
}
table.getFieldTypes().forEach((k, v) -> curEnv.define(new Symbol(Namespace.FIELD_NAME, k), v));
table.getReservedFieldTypes().forEach(
(k, v) -> curEnv.addReservedWord(new Symbol(Namespace.FIELD_NAME, k), v)
);
table
.getReservedFieldTypes()
.forEach((k, v) -> curEnv.define(new Symbol(Namespace.HIDDEN_FIELD_NAME, k), v));

// Put index name or its alias in index namespace on type environment so qualifier
// can be removed when analyzing qualified name. The value (expr type) here doesn't matter.
Expand Down Expand Up @@ -207,12 +207,15 @@ public LogicalPlan visitTableFunction(TableFunction node, AnalysisContext contex
TypeEnvironment curEnv = context.peek();
Table table = tableFunctionImplementation.applyArguments();
table.getFieldTypes().forEach((k, v) -> curEnv.define(new Symbol(Namespace.FIELD_NAME, k), v));
table.getReservedFieldTypes().forEach(
(k, v) -> curEnv.addReservedWord(new Symbol(Namespace.FIELD_NAME, k), v)
);
curEnv.define(new Symbol(Namespace.INDEX_NAME,
dataSourceSchemaIdentifierNameResolver.getIdentifierName()), STRUCT);
return new LogicalRelation(dataSourceSchemaIdentifierNameResolver.getIdentifierName(),
table
.getReservedFieldTypes()
.forEach((k, v) -> curEnv.define(new Symbol(Namespace.HIDDEN_FIELD_NAME, k), v));
curEnv.define(
new Symbol(
Namespace.INDEX_NAME, dataSourceSchemaIdentifierNameResolver.getIdentifierName()),
STRUCT);
return new LogicalRelation(
dataSourceSchemaIdentifierNameResolver.getIdentifierName(),
tableFunctionImplementation.applyArguments());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,10 @@ public Expression visitQualifiedName(QualifiedName node, AnalysisContext context
// check for reserved words in the identifier
for (String part : node.getParts()) {
for (TypeEnvironment typeEnv = context.peek();
typeEnv != null;
typeEnv = typeEnv.getParent()) {
Optional<ExprType> exprType = typeEnv.getReservedSymbolTable().lookup(
new Symbol(Namespace.FIELD_NAME, part));
typeEnv != null;
typeEnv = typeEnv.getParent()) {
Optional<ExprType> exprType =
Optional.ofNullable(typeEnv.lookupAllFields(Namespace.HIDDEN_FIELD_NAME).get(part));
if (exprType.isPresent()) {
return visitMetadata(
qualifierAnalyzer.unqualified(node),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public class TypeEnvironment implements Environment<Symbol, ExprType> {
private final TypeEnvironment parent;
private final SymbolTable symbolTable;

@Getter
private final SymbolTable reservedSymbolTable;

/**
* Constructor with empty symbol tables.
*
Expand All @@ -40,7 +37,6 @@ public class TypeEnvironment implements Environment<Symbol, ExprType> {
public TypeEnvironment(TypeEnvironment parent) {
this.parent = parent;
this.symbolTable = new SymbolTable();
this.reservedSymbolTable = new SymbolTable();
}

/**
Expand All @@ -52,7 +48,6 @@ public TypeEnvironment(TypeEnvironment parent) {
public TypeEnvironment(TypeEnvironment parent, SymbolTable symbolTable) {
this.parent = parent;
this.symbolTable = symbolTable;
this.reservedSymbolTable = new SymbolTable();
}

/**
Expand Down Expand Up @@ -133,8 +128,4 @@ public void clearAllFields() {
lookupAllFields(FIELD_NAME).keySet().forEach(
v -> remove(new Symbol(Namespace.FIELD_NAME, v)));
}

public void addReservedWord(Symbol symbol, ExprType type) {
reservedSymbolTable.store(symbol, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum Namespace {

INDEX_NAME("Index"),
FIELD_NAME("Field"),
HIDDEN_FIELD_NAME("HiddenField"),
FUNCTION_NAME("Function");

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ public void qualified_name_with_qualifier() {
public void qualified_name_with_reserved_symbol() {
analysisContext.push();

analysisContext.peek().addReservedWord(new Symbol(Namespace.FIELD_NAME, "_reserved"), STRING);
analysisContext.peek().addReservedWord(new Symbol(Namespace.FIELD_NAME, "_priority"), FLOAT);
analysisContext.peek().define(new Symbol(Namespace.HIDDEN_FIELD_NAME, "_reserved"), STRING);
analysisContext.peek().define(new Symbol(Namespace.HIDDEN_FIELD_NAME, "_priority"), FLOAT);
analysisContext.peek().define(new Symbol(Namespace.INDEX_NAME, "index_alias"), STRUCT);
assertAnalyzeEqual(
DSL.ref("_priority", FLOAT),
Expand All @@ -246,7 +246,7 @@ public void qualified_name_with_reserved_symbol() {
qualifiedName("index_alias", "_reserved")
);

// reserved fields take priority over symbol table
// cannot replace an existing field type
analysisContext.peek().define(new Symbol(Namespace.FIELD_NAME, "_reserved"), LONG);
assertAnalyzeEqual(
DSL.ref("_reserved", STRING),
Expand Down

0 comments on commit 0b419ff

Please sign in to comment.