Skip to content

SQL: Remove the deprecated AttributeMap(Map) calls #64664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.function.BiConsumer;
import java.util.stream.Stream;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static java.util.Collections.unmodifiableCollection;
import static java.util.Collections.unmodifiableSet;
Expand Down Expand Up @@ -141,7 +140,7 @@ public String toString() {
}

@SuppressWarnings("rawtypes")
public static final AttributeMap EMPTY = new AttributeMap<>();
private static final AttributeMap EMPTY = new AttributeMap<>();

@SuppressWarnings("unchecked")
public static final <E> AttributeMap<E> emptyAttributeMap() {
Expand All @@ -157,23 +156,6 @@ public AttributeMap() {
delegate = new LinkedHashMap<>();
}

/**
* Please use the {@link AttributeMap#builder()} instead.
*/
@Deprecated
public AttributeMap(Map<Attribute, E> attr) {
if (attr.isEmpty()) {
delegate = emptyMap();
}
else {
delegate = new LinkedHashMap<>(attr.size());

for (Entry<Attribute, E> entry : attr.entrySet()) {
delegate.put(new AttributeWrapper(entry.getKey()), entry.getValue());
}
}
}

public AttributeMap(Attribute key, E value) {
delegate = singletonMap(new AttributeWrapper(key), value);
}
Expand Down Expand Up @@ -377,6 +359,10 @@ public static <E> Builder<E> builder() {
return new Builder<>();
}

public static <E> Builder<E> builder(AttributeMap<E> map) {
return new Builder<E>().putAll(map);
}

public static class Builder<E> {
private final AttributeMap<E> map = new AttributeMap<>();

Expand All @@ -393,7 +379,10 @@ public Builder<E> putAll(AttributeMap<E> m) {
}

public AttributeMap<E> build() {
return new AttributeMap<>(map);
// copy, in case someone would do a .build, .put, .build sequence
AttributeMap<E> m = new AttributeMap<>();
m.addAll(map);
return m;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not return the map directly? AttributeMap is immutable.
To guarantee that the builder is not used after build() is called, a flag could be used instead of copying things over.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed, won't copy on .build(), only will copy once on the first .put() after a .build().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the defensive copy completely: c4e3fc4

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
import java.util.function.Predicate;
import java.util.stream.Stream;

import static java.util.Collections.emptyMap;

public class AttributeSet implements Set<Attribute> {

private static final AttributeMap<Object> EMPTY_DELEGATE = new AttributeMap<>(emptyMap());
private static final AttributeMap<Object> EMPTY_DELEGATE = AttributeMap.emptyAttributeMap();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.function.Predicate;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;

public final class Expressions {

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

public static AttributeMap<Expression> asAttributeMap(List<? extends NamedExpression> named) {
if (named.isEmpty()) {
return new AttributeMap<>(emptyMap());
return AttributeMap.emptyAttributeMap();
}

AttributeMap<Expression> map = new AttributeMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.elasticsearch.xpack.ql.type.DataTypes;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -30,12 +29,12 @@ private static Attribute a(String name) {
}

private static AttributeMap<String> threeMap() {
Map<Attribute, String> map = new LinkedHashMap<>();
map.put(a("one"), "one");
map.put(a("two"), "two");
map.put(a("three"), "three");
AttributeMap.Builder<String> builder = AttributeMap.builder();
builder.put(a("one"), "one");
builder.put(a("two"), "two");
builder.put(a("three"), "three");

return new AttributeMap<>(map);
return builder.build();
}

public void testAttributeMapWithSameAliasesCanResolveAttributes() {
Expand All @@ -49,27 +48,16 @@ public void testAttributeMapWithSameAliasesCanResolveAttributes() {
assertTrue(param1.toAttribute().equals(param2.toAttribute()));
assertFalse(param1.toAttribute().semanticEquals(param2.toAttribute()));

Map<Attribute, Expression> collectRefs = new LinkedHashMap<>();
for (Alias a : List.of(param1, param2)) {
collectRefs.put(a.toAttribute(), a.child());
}
// we can look up the same item by both attributes
assertNotNull(collectRefs.get(param1.toAttribute()));
assertNotNull(collectRefs.get(param2.toAttribute()));
AttributeMap<Expression> attributeMap = new AttributeMap<>(collectRefs);

// validate that all Alias can be e
assertTrue(attributeMap.containsKey(param1.toAttribute()));
assertFalse(attributeMap.containsKey(param2.toAttribute())); // results in unknown attribute exception

AttributeMap.Builder<Expression> mapBuilder = AttributeMap.builder();
for (Alias a : List.of(param1, param2)) {
mapBuilder.put(a.toAttribute(), a.child());
}
AttributeMap<Expression> newAttributeMap = mapBuilder.build();

assertTrue(newAttributeMap.containsKey(param1.toAttribute()));
assertTrue(newAttributeMap.containsKey(param2.toAttribute())); // no more unknown attribute exception
assertTrue(newAttributeMap.get(param1.toAttribute()) == param1.child());
assertTrue(newAttributeMap.containsKey(param2.toAttribute()));
assertTrue(newAttributeMap.get(param2.toAttribute()) == param2.child());
}

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

public void testMapConstructor() {
Map<Attribute, String> map = new LinkedHashMap<>();
map.put(a("one"), "one");
map.put(a("two"), "two");
map.put(a("three"), "three");
public void testBuilder() {
AttributeMap.Builder<String> builder = AttributeMap.builder();
builder.put(a("one"), "one");
builder.put(a("two"), "two");
builder.put(a("three"), "three");

AttributeMap<String> m = new AttributeMap<>(map);
AttributeMap<String> m = builder.build();
assertThat(m.size(), is(3));
assertThat(m.isEmpty(), is(false));

Expand All @@ -101,10 +89,10 @@ public void testMapConstructor() {
assertThat(m.containsValue("one"), is(true));
assertThat(m.containsValue("on"), is(false));
assertThat(m.attributeNames(), contains("one", "two", "three"));
assertThat(m.values(), contains(map.values().toArray()));
assertThat(m.values(), contains("one", "two", "three"));

// defensive copying
map.put(a("four"), "four");
builder.put(a("four"), "four");
assertThat(m.size(), is(3));
assertThat(m.isEmpty(), is(false));
}
Expand Down Expand Up @@ -164,12 +152,7 @@ public void testKeySet() {
Attribute two = a("two");
Attribute three = a("three");

Map<Attribute, String> map = new LinkedHashMap<>();
map.put(one, "one");
map.put(two, "two");
map.put(three, "three");

Set<Attribute> keySet = new AttributeMap<>(map).keySet();
Set<Attribute> keySet = threeMap().keySet();
assertThat(keySet, contains(one, two, three));

// toObject
Expand All @@ -192,12 +175,7 @@ public void testEntrySet() {
Attribute two = a("two");
Attribute three = a("three");

Map<Attribute, String> map = new LinkedHashMap<>();
map.put(one, "one");
map.put(two, "two");
map.put(three, "three");

Set<Entry<Attribute, String>> set = new AttributeMap<>(map).entrySet();
Set<Entry<Attribute, String>> set = threeMap().entrySet();

assertThat(set, hasSize(3));

Expand All @@ -211,12 +189,9 @@ public void testEntrySet() {
assertThat(values, contains("one", "two", "three"));
}

public void testForEach() {
public void testCopy() {
AttributeMap<String> m = threeMap();

Map<Attribute, String> collect = new LinkedHashMap<>();
m.forEach(collect::put);
AttributeMap<String> copy = new AttributeMap<>(collect);
AttributeMap<String> copy = AttributeMap.<String>builder().putAll(m).build();

assertThat(m, is(copy));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ protected LogicalPlan rule(Project project) {
OrderBy ob = (OrderBy) project.child();

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

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

AttributeMap<Function> functions = new AttributeMap<>(collectRefs);
AttributeMap<Function> functions = collectRefs.build();

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

// collect aliases in the lower list
Map<Attribute, NamedExpression> map = new LinkedHashMap<>();
AttributeMap.Builder<NamedExpression> aliasesBuilder = AttributeMap.builder();
for (NamedExpression ne : lower) {
if ((ne instanceof Attribute) == false) {
map.put(ne.toAttribute(), ne);
aliasesBuilder.put(ne.toAttribute(), ne);
}
}

AttributeMap<NamedExpression> aliases = new AttributeMap<>(map);
AttributeMap<NamedExpression> aliases = aliasesBuilder.build();
List<NamedExpression> replaced = new ArrayList<>();

// replace any matching attribute with a lower alias (if there's a match)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

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

public AttributeMap<Literal> valuesToLiterals() {
AttributeSet outValues = valuesOutput();
Map<Attribute, Literal> valuesMap = new LinkedHashMap<>();
AttributeMap.Builder<Literal> valuesMap = AttributeMap.builder();

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

return new AttributeMap<>(valuesMap);
return valuesMap.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,20 +421,21 @@ static EsQueryExec fold(AggregateExec a, EsQueryExec exec) {

// track aliases defined in the SELECT and used inside GROUP BY
// SELECT x AS a ... GROUP BY a
Map<Attribute, Expression> aliasMap = new LinkedHashMap<>();
String id = null;

AttributeMap.Builder<Expression> aliases = AttributeMap.builder();
for (NamedExpression ne : a.aggregates()) {
if (ne instanceof Alias) {
aliasMap.put(ne.toAttribute(), ((Alias) ne).child());
aliases.put(ne.toAttribute(), ((Alias) ne).child());
}
}

if (aliasMap.isEmpty() == false) {
Map<Attribute, Expression> newAliases = new LinkedHashMap<>(queryC.aliases());
newAliases.putAll(aliasMap);
queryC = queryC.withAliases(new AttributeMap<>(newAliases));
if (aliases.build().isEmpty() == false) {
aliases.putAll(queryC.aliases());
queryC = queryC.withAliases(aliases.build());
}


// build the group aggregation
// NB: any reference in grouping is already "optimized" by its source so there's no need to look for aliases
GroupingContext groupingContext = groupBy(a.groupings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,8 @@ public FieldExtraction resolve(Attribute attribute) {

// update proc (if needed)
if (qContainer.scalarFunctions().size() != scalarFunctions.size()) {
Map<Attribute, Pipe> procs = new LinkedHashMap<>(qContainer.scalarFunctions());
procs.put(attr, proc);
qContainer = qContainer.withScalarProcessors(new AttributeMap<>(procs));
qContainer = qContainer.withScalarProcessors(
AttributeMap.builder(qContainer.scalarFunctions).put(attr, proc).build());
}

return new Tuple<>(qContainer, new ComputedRef(proc));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.ql.expression.Attribute;
import org.elasticsearch.xpack.ql.expression.AttributeMap;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.expression.ReferenceAttribute;
import org.elasticsearch.xpack.ql.querydsl.container.AttributeSort;
Expand All @@ -32,9 +30,6 @@
import org.elasticsearch.xpack.sql.querydsl.container.QueryContainer;
import org.elasticsearch.xpack.sql.querydsl.container.ScoreSort;

import java.util.LinkedHashMap;
import java.util.Map;

import static java.util.Collections.singletonList;
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
Expand Down Expand Up @@ -89,9 +84,7 @@ public void testSortNoneSpecified() {
public void testSelectScoreForcesTrackingScore() {
Score score = new Score(Source.EMPTY);
ReferenceAttribute attr = new ReferenceAttribute(score.source(), "score", score.dataType());
Map<Attribute, Expression> alias = new LinkedHashMap<>();
alias.put(attr, score);
QueryContainer container = new QueryContainer().withAliases(new AttributeMap<>(alias)).addColumn(attr);
QueryContainer container = new QueryContainer().withAliases(new AttributeMap<>(attr, score)).addColumn(attr);
SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder(container, null, randomIntBetween(1, 10));
assertTrue(sourceBuilder.trackScores());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.elasticsearch.xpack.ql.expression.Alias;
import org.elasticsearch.xpack.ql.expression.Attribute;
import org.elasticsearch.xpack.ql.expression.AttributeMap;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.querydsl.query.BoolQuery;
import org.elasticsearch.xpack.ql.querydsl.query.MatchAll;
Expand All @@ -24,8 +23,6 @@
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Arrays;
import java.util.BitSet;
import java.util.LinkedHashMap;
import java.util.Map;

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

Map<Attribute, Expression> aliasesMap = new LinkedHashMap<>();
aliasesMap.put(firstAliased.toAttribute(), first);

QueryContainer queryContainer = new QueryContainer()
.withAliases(new AttributeMap<>(aliasesMap))
.withAliases(new AttributeMap<>(firstAliased.toAttribute(), first))
.addColumn(third)
.addColumn(first)
.addColumn(fourth)
Expand Down