Skip to content

Commit 8f45694

Browse files
author
Andras Palinkas
authored
SQL: Remove the deprecated AttributeMap(Map) calls (#64664) (#64815)
* SQL: Remove the deprecated `AttributeMap(Map)` calls (#64664) Use the `AttributeMap.builder()` instead of the `AttributeMap(Map)` to construct immutable `AttributeMap`s. Clean up after the `ctor` deprecation in #63710
1 parent 724b69f commit 8f45694

File tree

10 files changed

+47
-109
lines changed

10 files changed

+47
-109
lines changed

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeMap.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.function.BiConsumer;
1616
import java.util.stream.Stream;
1717

18-
import static java.util.Collections.emptyMap;
1918
import static java.util.Collections.singletonMap;
2019
import static java.util.Collections.unmodifiableCollection;
2120
import static java.util.Collections.unmodifiableSet;
@@ -141,8 +140,8 @@ public String toString() {
141140
}
142141

143142
@SuppressWarnings("rawtypes")
144-
public static final AttributeMap EMPTY = new AttributeMap<>();
145-
143+
private static final AttributeMap EMPTY = new AttributeMap<>();
144+
146145
@SuppressWarnings("unchecked")
147146
public static final <E> AttributeMap<E> emptyAttributeMap() {
148147
return EMPTY;
@@ -157,23 +156,6 @@ public AttributeMap() {
157156
delegate = new LinkedHashMap<>();
158157
}
159158

160-
/**
161-
* Please use the {@link AttributeMap#builder()} instead.
162-
*/
163-
@Deprecated
164-
public AttributeMap(Map<Attribute, E> attr) {
165-
if (attr.isEmpty()) {
166-
delegate = emptyMap();
167-
}
168-
else {
169-
delegate = new LinkedHashMap<>(attr.size());
170-
171-
for (Entry<Attribute, E> entry : attr.entrySet()) {
172-
delegate.put(new AttributeWrapper(entry.getKey()), entry.getValue());
173-
}
174-
}
175-
}
176-
177159
public AttributeMap(Attribute key, E value) {
178160
delegate = singletonMap(new AttributeWrapper(key), value);
179161
}
@@ -377,8 +359,12 @@ public static <E> Builder<E> builder() {
377359
return new Builder<>();
378360
}
379361

362+
public static <E> Builder<E> builder(AttributeMap<E> map) {
363+
return new Builder<E>().putAll(map);
364+
}
365+
380366
public static class Builder<E> {
381-
private final AttributeMap<E> map = new AttributeMap<>();
367+
private AttributeMap<E> map = new AttributeMap<>();
382368

383369
private Builder() {}
384370

@@ -393,7 +379,7 @@ public Builder<E> putAll(AttributeMap<E> m) {
393379
}
394380

395381
public AttributeMap<E> build() {
396-
return new AttributeMap<>(map);
382+
return map;
397383
}
398384
}
399385
}

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeSet.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
import java.util.function.Predicate;
1414
import java.util.stream.Stream;
1515

16-
import static java.util.Collections.emptyMap;
17-
1816
public class AttributeSet implements Set<Attribute> {
1917

20-
private static final AttributeMap<Object> EMPTY_DELEGATE = new AttributeMap<>(emptyMap());
18+
private static final AttributeMap<Object> EMPTY_DELEGATE = AttributeMap.emptyAttributeMap();
2119

2220
public static final AttributeSet EMPTY = new AttributeSet(EMPTY_DELEGATE);
2321

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Expressions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.function.Predicate;
2222

2323
import static java.util.Collections.emptyList;
24-
import static java.util.Collections.emptyMap;
2524

2625
public final class Expressions {
2726

@@ -65,7 +64,7 @@ public static List<Attribute> asAttributes(List<? extends NamedExpression> named
6564

6665
public static AttributeMap<Expression> asAttributeMap(List<? extends NamedExpression> named) {
6766
if (named.isEmpty()) {
68-
return new AttributeMap<>(emptyMap());
67+
return AttributeMap.emptyAttributeMap();
6968
}
7069

7170
AttributeMap<Expression> map = new AttributeMap<>();

x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/AttributeMapTests.java

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import java.util.Arrays;
1313
import java.util.Collection;
14-
import java.util.LinkedHashMap;
1514
import java.util.List;
1615
import java.util.Map;
1716
import java.util.Map.Entry;
@@ -31,12 +30,12 @@ private static Attribute a(String name) {
3130
}
3231

3332
private static AttributeMap<String> threeMap() {
34-
Map<Attribute, String> map = new LinkedHashMap<>();
35-
map.put(a("one"), "one");
36-
map.put(a("two"), "two");
37-
map.put(a("three"), "three");
33+
AttributeMap.Builder<String> builder = AttributeMap.builder();
34+
builder.put(a("one"), "one");
35+
builder.put(a("two"), "two");
36+
builder.put(a("three"), "three");
3837

39-
return new AttributeMap<>(map);
38+
return builder.build();
4039
}
4140

4241
public void testAttributeMapWithSameAliasesCanResolveAttributes() {
@@ -50,27 +49,16 @@ public void testAttributeMapWithSameAliasesCanResolveAttributes() {
5049
assertTrue(param1.toAttribute().equals(param2.toAttribute()));
5150
assertFalse(param1.toAttribute().semanticEquals(param2.toAttribute()));
5251

53-
Map<Attribute, Expression> collectRefs = new LinkedHashMap<>();
54-
for (Alias a : Arrays.asList(param1, param2)) {
55-
collectRefs.put(a.toAttribute(), a.child());
56-
}
57-
// we can look up the same item by both attributes
58-
assertNotNull(collectRefs.get(param1.toAttribute()));
59-
assertNotNull(collectRefs.get(param2.toAttribute()));
60-
AttributeMap<Expression> attributeMap = new AttributeMap<>(collectRefs);
61-
62-
// validate that all Alias can be e
63-
assertTrue(attributeMap.containsKey(param1.toAttribute()));
64-
assertFalse(attributeMap.containsKey(param2.toAttribute())); // results in unknown attribute exception
65-
6652
AttributeMap.Builder<Expression> mapBuilder = AttributeMap.builder();
6753
for (Alias a : Arrays.asList(param1, param2)) {
6854
mapBuilder.put(a.toAttribute(), a.child());
6955
}
7056
AttributeMap<Expression> newAttributeMap = mapBuilder.build();
7157

7258
assertTrue(newAttributeMap.containsKey(param1.toAttribute()));
73-
assertTrue(newAttributeMap.containsKey(param2.toAttribute())); // no more unknown attribute exception
59+
assertTrue(newAttributeMap.get(param1.toAttribute()) == param1.child());
60+
assertTrue(newAttributeMap.containsKey(param2.toAttribute()));
61+
assertTrue(newAttributeMap.get(param2.toAttribute()) == param2.child());
7462
}
7563

7664
private Alias createIntParameterAlias(int index, int value) {
@@ -86,13 +74,13 @@ public void testEmptyConstructor() {
8674
assertThat(m.isEmpty(), is(true));
8775
}
8876

89-
public void testMapConstructor() {
90-
Map<Attribute, String> map = new LinkedHashMap<>();
91-
map.put(a("one"), "one");
92-
map.put(a("two"), "two");
93-
map.put(a("three"), "three");
77+
public void testBuilder() {
78+
AttributeMap.Builder<String> builder = AttributeMap.builder();
79+
builder.put(a("one"), "one");
80+
builder.put(a("two"), "two");
81+
builder.put(a("three"), "three");
9482

95-
AttributeMap<String> m = new AttributeMap<>(map);
83+
AttributeMap<String> m = builder.build();
9684
assertThat(m.size(), is(3));
9785
assertThat(m.isEmpty(), is(false));
9886

@@ -102,12 +90,7 @@ public void testMapConstructor() {
10290
assertThat(m.containsValue("one"), is(true));
10391
assertThat(m.containsValue("on"), is(false));
10492
assertThat(m.attributeNames(), contains("one", "two", "three"));
105-
assertThat(m.values(), contains(map.values().toArray()));
106-
107-
// defensive copying
108-
map.put(a("four"), "four");
109-
assertThat(m.size(), is(3));
110-
assertThat(m.isEmpty(), is(false));
93+
assertThat(m.values(), contains("one", "two", "three"));
11194
}
11295

11396
public void testSingleItemConstructor() {
@@ -165,12 +148,7 @@ public void testKeySet() {
165148
Attribute two = a("two");
166149
Attribute three = a("three");
167150

168-
Map<Attribute, String> map = new LinkedHashMap<>();
169-
map.put(one, "one");
170-
map.put(two, "two");
171-
map.put(three, "three");
172-
173-
Set<Attribute> keySet = new AttributeMap<>(map).keySet();
151+
Set<Attribute> keySet = threeMap().keySet();
174152
assertThat(keySet, contains(one, two, three));
175153

176154
// toObject
@@ -193,12 +171,7 @@ public void testEntrySet() {
193171
Attribute two = a("two");
194172
Attribute three = a("three");
195173

196-
Map<Attribute, String> map = new LinkedHashMap<>();
197-
map.put(one, "one");
198-
map.put(two, "two");
199-
map.put(three, "three");
200-
201-
Set<Entry<Attribute, String>> set = new AttributeMap<>(map).entrySet();
174+
Set<Entry<Attribute, String>> set = threeMap().entrySet();
202175

203176
assertThat(set, hasSize(3));
204177

@@ -212,12 +185,9 @@ public void testEntrySet() {
212185
assertThat(values, contains("one", "two", "three"));
213186
}
214187

215-
public void testForEach() {
188+
public void testCopy() {
216189
AttributeMap<String> m = threeMap();
217-
218-
Map<Attribute, String> collect = new LinkedHashMap<>();
219-
m.forEach(collect::put);
220-
AttributeMap<String> copy = new AttributeMap<>(collect);
190+
AttributeMap<String> copy = AttributeMap.<String>builder().putAll(m).build();
221191

222192
assertThat(m, is(copy));
223193
}

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ protected LogicalPlan rule(Project project) {
302302
OrderBy ob = (OrderBy) project.child();
303303

304304
// resolve function references (that maybe hiding the target)
305-
final Map<Attribute, Function> collectRefs = new LinkedHashMap<>();
305+
AttributeMap.Builder<Function> collectRefs = AttributeMap.builder();
306306

307307
// collect Attribute sources
308308
// only Aliases are interesting since these are the only ones that hide expressions
@@ -316,7 +316,7 @@ protected LogicalPlan rule(Project project) {
316316
}
317317
}));
318318

319-
AttributeMap<Function> functions = new AttributeMap<>(collectRefs);
319+
AttributeMap<Function> functions = collectRefs.build();
320320

321321
// track the direct parents
322322
Map<String, Order> nestedOrders = new LinkedHashMap<>();
@@ -541,14 +541,14 @@ private List<NamedExpression> combineProjections(List<? extends NamedExpression>
541541
//TODO: this need rewriting when moving functions of NamedExpression
542542

543543
// collect aliases in the lower list
544-
Map<Attribute, NamedExpression> map = new LinkedHashMap<>();
544+
AttributeMap.Builder<NamedExpression> aliasesBuilder = AttributeMap.builder();
545545
for (NamedExpression ne : lower) {
546546
if ((ne instanceof Attribute) == false) {
547-
map.put(ne.toAttribute(), ne);
547+
aliasesBuilder.put(ne.toAttribute(), ne);
548548
}
549549
}
550550

551-
AttributeMap<NamedExpression> aliases = new AttributeMap<>(map);
551+
AttributeMap<NamedExpression> aliases = aliasesBuilder.build();
552552
List<NamedExpression> replaced = new ArrayList<>();
553553

554554
// replace any matching attribute with a lower alias (if there's a match)

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Pivot.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
2424

2525
import java.util.ArrayList;
26-
import java.util.LinkedHashMap;
2726
import java.util.List;
28-
import java.util.Map;
2927
import java.util.Objects;
3028

3129
import static java.util.Collections.singletonList;
@@ -134,7 +132,7 @@ private AttributeSet valuesOutput() {
134132

135133
public AttributeMap<Literal> valuesToLiterals() {
136134
AttributeSet outValues = valuesOutput();
137-
Map<Attribute, Literal> valuesMap = new LinkedHashMap<>();
135+
AttributeMap.Builder<Literal> valuesMap = AttributeMap.builder();
138136

139137
int index = 0;
140138
// for each attribute, associate its value
@@ -152,7 +150,7 @@ public AttributeMap<Literal> valuesToLiterals() {
152150
}
153151
}
154152

155-
return new AttributeMap<>(valuesMap);
153+
return valuesMap.build();
156154
}
157155

158156
@Override

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryFolder.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,20 +421,21 @@ static EsQueryExec fold(AggregateExec a, EsQueryExec exec) {
421421

422422
// track aliases defined in the SELECT and used inside GROUP BY
423423
// SELECT x AS a ... GROUP BY a
424-
Map<Attribute, Expression> aliasMap = new LinkedHashMap<>();
425424
String id = null;
425+
426+
AttributeMap.Builder<Expression> aliases = AttributeMap.builder();
426427
for (NamedExpression ne : a.aggregates()) {
427428
if (ne instanceof Alias) {
428-
aliasMap.put(ne.toAttribute(), ((Alias) ne).child());
429+
aliases.put(ne.toAttribute(), ((Alias) ne).child());
429430
}
430431
}
431432

432-
if (aliasMap.isEmpty() == false) {
433-
Map<Attribute, Expression> newAliases = new LinkedHashMap<>(queryC.aliases());
434-
newAliases.putAll(aliasMap);
435-
queryC = queryC.withAliases(new AttributeMap<>(newAliases));
433+
if (aliases.build().isEmpty() == false) {
434+
aliases.putAll(queryC.aliases());
435+
queryC = queryC.withAliases(aliases.build());
436436
}
437437

438+
438439
// build the group aggregation
439440
// NB: any reference in grouping is already "optimized" by its source so there's no need to look for aliases
440441
GroupingContext groupingContext = groupBy(a.groupings());

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,8 @@ public FieldExtraction resolve(Attribute attribute) {
432432

433433
// update proc (if needed)
434434
if (qContainer.scalarFunctions().size() != scalarFunctions.size()) {
435-
Map<Attribute, Pipe> procs = new LinkedHashMap<>(qContainer.scalarFunctions());
436-
procs.put(attr, proc);
437-
qContainer = qContainer.withScalarProcessors(new AttributeMap<>(procs));
435+
qContainer = qContainer.withScalarProcessors(
436+
AttributeMap.builder(qContainer.scalarFunctions).put(attr, proc).build());
438437
}
439438

440439
return new Tuple<>(qContainer, new ComputedRef(proc));

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SourceGeneratorTests.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
import org.elasticsearch.search.sort.FieldSortBuilder;
1515
import org.elasticsearch.search.sort.SortOrder;
1616
import org.elasticsearch.test.ESTestCase;
17-
import org.elasticsearch.xpack.ql.expression.Attribute;
1817
import org.elasticsearch.xpack.ql.expression.AttributeMap;
19-
import org.elasticsearch.xpack.ql.expression.Expression;
2018
import org.elasticsearch.xpack.ql.expression.FieldAttribute;
2119
import org.elasticsearch.xpack.ql.expression.ReferenceAttribute;
2220
import org.elasticsearch.xpack.ql.querydsl.container.AttributeSort;
@@ -32,9 +30,6 @@
3230
import org.elasticsearch.xpack.sql.querydsl.container.QueryContainer;
3331
import org.elasticsearch.xpack.sql.querydsl.container.ScoreSort;
3432

35-
import java.util.LinkedHashMap;
36-
import java.util.Map;
37-
3833
import static java.util.Collections.singletonList;
3934
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
4035
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
@@ -89,9 +84,7 @@ public void testSortNoneSpecified() {
8984
public void testSelectScoreForcesTrackingScore() {
9085
Score score = new Score(Source.EMPTY);
9186
ReferenceAttribute attr = new ReferenceAttribute(score.source(), "score", score.dataType());
92-
Map<Attribute, Expression> alias = new LinkedHashMap<>();
93-
alias.put(attr, score);
94-
QueryContainer container = new QueryContainer().withAliases(new AttributeMap<>(alias)).addColumn(attr);
87+
QueryContainer container = new QueryContainer().withAliases(new AttributeMap<>(attr, score)).addColumn(attr);
9588
SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder(container, null, randomIntBetween(1, 10));
9689
assertTrue(sourceBuilder.trackScores());
9790
}

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainerTests.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.elasticsearch.xpack.ql.expression.Alias;
1010
import org.elasticsearch.xpack.ql.expression.Attribute;
1111
import org.elasticsearch.xpack.ql.expression.AttributeMap;
12-
import org.elasticsearch.xpack.ql.expression.Expression;
1312
import org.elasticsearch.xpack.ql.expression.FieldAttribute;
1413
import org.elasticsearch.xpack.ql.querydsl.query.BoolQuery;
1514
import org.elasticsearch.xpack.ql.querydsl.query.MatchAll;
@@ -24,8 +23,6 @@
2423
import java.util.AbstractMap.SimpleImmutableEntry;
2524
import java.util.Arrays;
2625
import java.util.BitSet;
27-
import java.util.LinkedHashMap;
28-
import java.util.Map;
2926

3027
import static java.util.Collections.emptyMap;
3128
import static java.util.Collections.singletonMap;
@@ -86,11 +83,8 @@ public void testColumnMaskShouldDuplicateSameAttributes() {
8683
Attribute fourth = new FieldAttribute(Source.EMPTY, "fourth", esField);
8784
Alias firstAliased = new Alias(Source.EMPTY, "firstAliased", first);
8885

89-
Map<Attribute, Expression> aliasesMap = new LinkedHashMap<>();
90-
aliasesMap.put(firstAliased.toAttribute(), first);
91-
9286
QueryContainer queryContainer = new QueryContainer()
93-
.withAliases(new AttributeMap<>(aliasesMap))
87+
.withAliases(new AttributeMap<>(firstAliased.toAttribute(), first))
9488
.addColumn(third)
9589
.addColumn(first)
9690
.addColumn(fourth)

0 commit comments

Comments
 (0)