1 | /* | |
2 | * Copyright OpenSearch Contributors | |
3 | * SPDX-License-Identifier: Apache-2.0 | |
4 | */ | |
5 | ||
6 | ||
7 | package org.opensearch.sql.analysis; | |
8 | ||
9 | import static org.opensearch.sql.analysis.symbol.Namespace.FIELD_NAME; | |
10 | ||
11 | import java.util.LinkedHashMap; | |
12 | import java.util.Map; | |
13 | import java.util.Optional; | |
14 | import lombok.Getter; | |
15 | import org.opensearch.sql.analysis.symbol.Namespace; | |
16 | import org.opensearch.sql.analysis.symbol.Symbol; | |
17 | import org.opensearch.sql.analysis.symbol.SymbolTable; | |
18 | import org.opensearch.sql.data.type.ExprType; | |
19 | import org.opensearch.sql.exception.SemanticCheckException; | |
20 | import org.opensearch.sql.expression.Expression; | |
21 | import org.opensearch.sql.expression.ReferenceExpression; | |
22 | import org.opensearch.sql.expression.env.Environment; | |
23 | ||
24 | /** | |
25 | * The definition of Type Environment. | |
26 | */ | |
27 | public class TypeEnvironment implements Environment<Symbol, ExprType> { | |
28 | @Getter | |
29 | private final TypeEnvironment parent; | |
30 | private final SymbolTable symbolTable; | |
31 | ||
32 | public TypeEnvironment(TypeEnvironment parent) { | |
33 | this.parent = parent; | |
34 | this.symbolTable = new SymbolTable(); | |
35 | } | |
36 | ||
37 | public TypeEnvironment(TypeEnvironment parent, SymbolTable symbolTable) { | |
38 | this.parent = parent; | |
39 | this.symbolTable = symbolTable; | |
40 | } | |
41 | ||
42 | /** | |
43 | * Resolve the {@link Expression} from environment. | |
44 | * | |
45 | * @param symbol Symbol | |
46 | * @return resolved {@link ExprType} | |
47 | */ | |
48 | @Override | |
49 | public ExprType resolve(Symbol symbol) { | |
50 |
1
1. resolve : negated conditional → KILLED |
for (TypeEnvironment cur = this; cur != null; cur = cur.parent) { |
51 | Optional<ExprType> typeOptional = cur.symbolTable.lookup(symbol); | |
52 |
1
1. resolve : negated conditional → KILLED |
if (typeOptional.isPresent()) { |
53 |
1
1. resolve : replaced return value with null for org/opensearch/sql/analysis/TypeEnvironment::resolve → KILLED |
return typeOptional.get(); |
54 | } | |
55 | } | |
56 | throw new SemanticCheckException( | |
57 | String.format("can't resolve %s in type env", symbol)); | |
58 | } | |
59 | ||
60 | /** | |
61 | * Resolve all fields in the current environment. | |
62 | * @param namespace a namespace | |
63 | * @return all symbols in the namespace | |
64 | */ | |
65 | public Map<String, ExprType> lookupAllFields(Namespace namespace) { | |
66 | Map<String, ExprType> result = new LinkedHashMap<>(); | |
67 |
1
1. lookupAllFields : removed call to java/util/Map::forEach → KILLED |
symbolTable.lookupAllFields(namespace).forEach(result::putIfAbsent); |
68 |
1
1. lookupAllFields : replaced return value with Collections.emptyMap for org/opensearch/sql/analysis/TypeEnvironment::lookupAllFields → KILLED |
return result; |
69 | } | |
70 | ||
71 | /** | |
72 | * Define symbol with the type. | |
73 | * | |
74 | * @param symbol symbol to define | |
75 | * @param type type | |
76 | */ | |
77 | public void define(Symbol symbol, ExprType type) { | |
78 |
1
1. define : removed call to org/opensearch/sql/analysis/symbol/SymbolTable::store → KILLED |
symbolTable.store(symbol, type); |
79 | } | |
80 | ||
81 | /** | |
82 | * Define expression with the type. | |
83 | * | |
84 | * @param ref {@link ReferenceExpression} | |
85 | */ | |
86 | public void define(ReferenceExpression ref) { | |
87 |
1
1. define : removed call to org/opensearch/sql/analysis/TypeEnvironment::define → KILLED |
define(new Symbol(FIELD_NAME, ref.getAttr()), ref.type()); |
88 | } | |
89 | ||
90 | public void remove(Symbol symbol) { | |
91 |
1
1. remove : removed call to org/opensearch/sql/analysis/symbol/SymbolTable::remove → KILLED |
symbolTable.remove(symbol); |
92 | } | |
93 | ||
94 | /** | |
95 | * Remove ref. | |
96 | */ | |
97 | public void remove(ReferenceExpression ref) { | |
98 |
1
1. remove : removed call to org/opensearch/sql/analysis/TypeEnvironment::remove → KILLED |
remove(new Symbol(FIELD_NAME, ref.getAttr())); |
99 | } | |
100 | ||
101 | /** | |
102 | * Clear all fields in the current environment. | |
103 | */ | |
104 | public void clearAllFields() { | |
105 | lookupAllFields(FIELD_NAME).keySet().stream() | |
106 |
2
1. clearAllFields : removed call to java/util/stream/Stream::forEach → SURVIVED 2. lambda$clearAllFields$0 : removed call to org/opensearch/sql/analysis/TypeEnvironment::remove → SURVIVED |
.forEach(v -> remove(new Symbol(Namespace.FIELD_NAME, v))); |
107 | } | |
108 | } | |
Mutations | ||
50 |
1.1 |
|
52 |
1.1 |
|
53 |
1.1 |
|
67 |
1.1 |
|
68 |
1.1 |
|
78 |
1.1 |
|
87 |
1.1 |
|
91 |
1.1 |
|
98 |
1.1 |
|
106 |
1.1 2.2 |