Skip to content

Commit 658e26d

Browse files
author
Andras Palinkas
authored
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 e86c65c commit 658e26d

File tree

10 files changed

+68
-107
lines changed

10 files changed

+68
-107
lines changed

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

Lines changed: 24 additions & 24 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,23 +359,41 @@ 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 = null;
368+
private AttributeMap<E> previouslyBuiltMap = null;
382369

383370
private Builder() {}
384371

372+
private AttributeMap<E> map() {
373+
if (map == null) {
374+
map = new AttributeMap<>();
375+
if (previouslyBuiltMap != null) {
376+
map.addAll(previouslyBuiltMap);
377+
}
378+
}
379+
return map;
380+
}
381+
385382
public Builder<E> put(Attribute attr, E value) {
386-
map.add(attr, value);
383+
map().add(attr, value);
387384
return this;
388385
}
389386

390387
public Builder<E> putAll(AttributeMap<E> m) {
391-
map.addAll(m);
388+
map().addAll(m);
392389
return this;
393390
}
394391

395392
public AttributeMap<E> build() {
396-
return new AttributeMap<>(map);
393+
AttributeMap<E> m = map();
394+
previouslyBuiltMap = m;
395+
map = null;
396+
return m;
397397
}
398398
}
399399
}

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: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.elasticsearch.xpack.ql.type.DataTypes;
1111

1212
import java.util.Collection;
13-
import java.util.LinkedHashMap;
1413
import java.util.List;
1514
import java.util.Map;
1615
import java.util.Map.Entry;
@@ -30,12 +29,12 @@ private static Attribute a(String name) {
3029
}
3130

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

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

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

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

7157
assertTrue(newAttributeMap.containsKey(param1.toAttribute()));
72-
assertTrue(newAttributeMap.containsKey(param2.toAttribute())); // no more unknown attribute exception
58+
assertTrue(newAttributeMap.get(param1.toAttribute()) == param1.child());
59+
assertTrue(newAttributeMap.containsKey(param2.toAttribute()));
60+
assertTrue(newAttributeMap.get(param2.toAttribute()) == param2.child());
7361
}
7462

7563
private Alias createIntParameterAlias(int index, int value) {
@@ -85,13 +73,13 @@ public void testEmptyConstructor() {
8573
assertThat(m.isEmpty(), is(true));
8674
}
8775

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

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

@@ -101,12 +89,16 @@ public void testMapConstructor() {
10189
assertThat(m.containsValue("one"), is(true));
10290
assertThat(m.containsValue("on"), is(false));
10391
assertThat(m.attributeNames(), contains("one", "two", "three"));
104-
assertThat(m.values(), contains(map.values().toArray()));
92+
assertThat(m.values(), contains("one", "two", "three"));
10593

10694
// defensive copying
107-
map.put(a("four"), "four");
95+
builder.put(a("four"), "four");
96+
AttributeMap<String> m2 = builder.build();
10897
assertThat(m.size(), is(3));
10998
assertThat(m.isEmpty(), is(false));
99+
assertThat(m2.size(), is(4));
100+
assertThat(m.isEmpty(), is(false));
101+
assertThat(m2.attributeNames(), contains("one", "two", "three", "four"));
110102
}
111103

112104
public void testSingleItemConstructor() {
@@ -164,12 +156,7 @@ public void testKeySet() {
164156
Attribute two = a("two");
165157
Attribute three = a("three");
166158

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

175162
// toObject
@@ -192,12 +179,7 @@ public void testEntrySet() {
192179
Attribute two = a("two");
193180
Attribute three = a("three");
194181

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

202184
assertThat(set, hasSize(3));
203185

@@ -211,12 +193,9 @@ public void testEntrySet() {
211193
assertThat(values, contains("one", "two", "three"));
212194
}
213195

214-
public void testForEach() {
196+
public void testCopy() {
215197
AttributeMap<String> m = threeMap();
216-
217-
Map<Attribute, String> collect = new LinkedHashMap<>();
218-
m.forEach(collect::put);
219-
AttributeMap<String> copy = new AttributeMap<>(collect);
198+
AttributeMap<String> copy = AttributeMap.<String>builder().putAll(m).build();
220199

221200
assertThat(m, is(copy));
222201
}

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
}

0 commit comments

Comments
 (0)