From bc270603ae86904ae68809c10345e3095889eaf0 Mon Sep 17 00:00:00 2001 From: acarbonetto Date: Thu, 3 Aug 2023 11:06:00 -0700 Subject: [PATCH 1/3] #1506: Remove reservedSymbolTable and use HIDDEN_FIELD_NAME instead Signed-off-by: acarbonetto --- .../main/java/org/opensearch/sql/analysis/Analyzer.java | 4 ++-- .../org/opensearch/sql/analysis/ExpressionAnalyzer.java | 6 ++++-- .../org/opensearch/sql/analysis/TypeEnvironment.java | 9 --------- .../org/opensearch/sql/analysis/symbol/Namespace.java | 1 + 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/opensearch/sql/analysis/Analyzer.java b/core/src/main/java/org/opensearch/sql/analysis/Analyzer.java index 2c4647004c..d89a0e4695 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/Analyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/Analyzer.java @@ -162,7 +162,7 @@ public LogicalPlan visitRelation(Relation node, AnalysisContext context) { } 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) + (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 @@ -208,7 +208,7 @@ public LogicalPlan visitTableFunction(TableFunction node, AnalysisContext contex 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) + (k, v) -> curEnv.define(new Symbol(Namespace.HIDDEN_FIELD_NAME, k), v) ); curEnv.define(new Symbol(Namespace.INDEX_NAME, dataSourceSchemaIdentifierNameResolver.getIdentifierName()), STRUCT); diff --git a/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java index 601e3e00cc..711c783a27 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java @@ -373,8 +373,10 @@ public Expression visitQualifiedName(QualifiedName node, AnalysisContext context for (TypeEnvironment typeEnv = context.peek(); typeEnv != null; typeEnv = typeEnv.getParent()) { - Optional exprType = typeEnv.getReservedSymbolTable().lookup( - new Symbol(Namespace.FIELD_NAME, part)); + Optional exprType = Optional.ofNullable( + typeEnv.lookupAllFields(Namespace.HIDDEN_FIELD_NAME).get(part)); +// Optional exprType = typeEnv.getReservedSymbolTable().lookup( +// new Symbol(Namespace.HIDDEN_FIELD_NAME, part)); if (exprType.isPresent()) { return visitMetadata( qualifierAnalyzer.unqualified(node), diff --git a/core/src/main/java/org/opensearch/sql/analysis/TypeEnvironment.java b/core/src/main/java/org/opensearch/sql/analysis/TypeEnvironment.java index 17d203f66f..8032cba706 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/TypeEnvironment.java +++ b/core/src/main/java/org/opensearch/sql/analysis/TypeEnvironment.java @@ -29,9 +29,6 @@ public class TypeEnvironment implements Environment { private final TypeEnvironment parent; private final SymbolTable symbolTable; - @Getter - private final SymbolTable reservedSymbolTable; - /** * Constructor with empty symbol tables. * @@ -40,7 +37,6 @@ public class TypeEnvironment implements Environment { public TypeEnvironment(TypeEnvironment parent) { this.parent = parent; this.symbolTable = new SymbolTable(); - this.reservedSymbolTable = new SymbolTable(); } /** @@ -52,7 +48,6 @@ public TypeEnvironment(TypeEnvironment parent) { public TypeEnvironment(TypeEnvironment parent, SymbolTable symbolTable) { this.parent = parent; this.symbolTable = symbolTable; - this.reservedSymbolTable = new SymbolTable(); } /** @@ -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); - } } diff --git a/core/src/main/java/org/opensearch/sql/analysis/symbol/Namespace.java b/core/src/main/java/org/opensearch/sql/analysis/symbol/Namespace.java index b5203033a8..32cb9dee35 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/symbol/Namespace.java +++ b/core/src/main/java/org/opensearch/sql/analysis/symbol/Namespace.java @@ -13,6 +13,7 @@ public enum Namespace { INDEX_NAME("Index"), FIELD_NAME("Field"), + HIDDEN_FIELD_NAME("HiddenField"), FUNCTION_NAME("Function"); private final String name; From dd3407eb311e016378e29877eff7d045eb8b67e5 Mon Sep 17 00:00:00 2001 From: acarbonetto Date: Thu, 3 Aug 2023 12:44:47 -0700 Subject: [PATCH 2/3] #1506: Remove reservedSymbolTable and use HIDDEN_FIELD_NAME instead Signed-off-by: acarbonetto --- .../org/opensearch/sql/analysis/ExpressionAnalyzerTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java b/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java index 5a05c79132..30a43f557f 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/ExpressionAnalyzerTest.java @@ -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), @@ -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), From 8438f5c9cd389a3fe0207c50f8e2ff73ff0ed53c Mon Sep 17 00:00:00 2001 From: acarbonetto Date: Thu, 3 Aug 2023 13:57:38 -0700 Subject: [PATCH 3/3] #1506: Fix checkstyle errors Signed-off-by: acarbonetto --- .../java/org/opensearch/sql/analysis/ExpressionAnalyzer.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java b/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java index d98ca98864..e4eb24804f 100644 --- a/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java +++ b/core/src/main/java/org/opensearch/sql/analysis/ExpressionAnalyzer.java @@ -374,8 +374,6 @@ public Expression visitQualifiedName(QualifiedName node, AnalysisContext context typeEnv = typeEnv.getParent()) { Optional exprType = Optional.ofNullable( typeEnv.lookupAllFields(Namespace.HIDDEN_FIELD_NAME).get(part)); -// Optional exprType = typeEnv.getReservedSymbolTable().lookup( -// new Symbol(Namespace.HIDDEN_FIELD_NAME, part)); if (exprType.isPresent()) { return visitMetadata( qualifierAnalyzer.unqualified(node),