diff --git a/presto-raptor/src/test/java/com/facebook/presto/raptor/metadata/TestShardPredicate.java b/presto-raptor/src/test/java/com/facebook/presto/raptor/metadata/TestShardPredicate.java index e28edfaff7c6d..acf132d6a2fb7 100644 --- a/presto-raptor/src/test/java/com/facebook/presto/raptor/metadata/TestShardPredicate.java +++ b/presto-raptor/src/test/java/com/facebook/presto/raptor/metadata/TestShardPredicate.java @@ -130,9 +130,11 @@ public void testMultipleColumnsMultipleRanges() new RaptorColumnHandle("test", "col", 1, INTEGER), create(SortedRangeSet.copyOf(INTEGER, ImmutableList.of(equal(INTEGER, 1L), equal(INTEGER, 3L))), false))); ShardPredicate shardPredicate = ShardPredicate.create(tupleDomain); - assertEquals(shardPredicate.getPredicate(), "(((c1_max >= ? OR c1_max IS NULL) AND (c1_min <= ? OR c1_min IS NULL)) " + - "OR ((c1_max >= ? OR c1_max IS NULL) AND (c1_min <= ? OR c1_min IS NULL))) " + - "AND (((bucket_number >= ? OR bucket_number IS NULL) AND (bucket_number <= ? OR bucket_number IS NULL)) " + - "OR ((bucket_number >= ? OR bucket_number IS NULL) AND (bucket_number <= ? OR bucket_number IS NULL)))"); + assertEquals( + shardPredicate.getPredicate(), + "(((bucket_number >= ? OR bucket_number IS NULL) AND (bucket_number <= ? OR bucket_number IS NULL)) " + + "OR ((bucket_number >= ? OR bucket_number IS NULL) AND (bucket_number <= ? OR bucket_number IS NULL))) " + + "AND (((c1_max >= ? OR c1_max IS NULL) AND (c1_min <= ? OR c1_min IS NULL)) " + + "OR ((c1_max >= ? OR c1_max IS NULL) AND (c1_min <= ? OR c1_min IS NULL)))"); } } diff --git a/presto-spi/src/main/java/com/facebook/presto/spi/GroupingProperty.java b/presto-spi/src/main/java/com/facebook/presto/spi/GroupingProperty.java index 85226af8cd43e..ce09b108c7a1c 100644 --- a/presto-spi/src/main/java/com/facebook/presto/spi/GroupingProperty.java +++ b/presto-spi/src/main/java/com/facebook/presto/spi/GroupingProperty.java @@ -17,15 +17,16 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import static java.util.Collections.unmodifiableSet; import static java.util.Objects.requireNonNull; +import static java.util.stream.Collectors.toCollection; public final class GroupingProperty implements LocalProperty @@ -37,7 +38,7 @@ public GroupingProperty(@JsonProperty("columns") Collection columns) { requireNonNull(columns, "columns is null"); - this.columns = Collections.unmodifiableSet(new HashSet<>(columns)); + this.columns = unmodifiableSet(new LinkedHashSet<>(columns)); } @Override @@ -76,12 +77,12 @@ public Optional> translate(Function> transla { Set> translated = columns.stream() .map(translator) - .collect(Collectors.toSet()); + .collect(toCollection(LinkedHashSet::new)); if (translated.stream().allMatch(Optional::isPresent)) { Set columns = translated.stream() .map(Optional::get) - .collect(Collectors.toSet()); + .collect(toCollection(LinkedHashSet::new)); return Optional.of(new GroupingProperty<>(columns)); } diff --git a/presto-spi/src/main/java/com/facebook/presto/spi/LocalProperty.java b/presto-spi/src/main/java/com/facebook/presto/spi/LocalProperty.java index 75f7728b9a03b..721f0c2f24552 100644 --- a/presto-spi/src/main/java/com/facebook/presto/spi/LocalProperty.java +++ b/presto-spi/src/main/java/com/facebook/presto/spi/LocalProperty.java @@ -16,7 +16,7 @@ import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Optional; import java.util.Set; import java.util.function.Function; @@ -48,7 +48,7 @@ public interface LocalProperty */ default Optional> withConstants(Set constants) { - Set set = new HashSet<>(getColumns()); + Set set = new LinkedHashSet<>(getColumns()); set.removeAll(constants); if (set.isEmpty()) { diff --git a/presto-spi/src/main/java/com/facebook/presto/spi/predicate/EquatableValueSet.java b/presto-spi/src/main/java/com/facebook/presto/spi/predicate/EquatableValueSet.java index a5012ecbe607a..fea4addd1b8f0 100644 --- a/presto-spi/src/main/java/com/facebook/presto/spi/predicate/EquatableValueSet.java +++ b/presto-spi/src/main/java/com/facebook/presto/spi/predicate/EquatableValueSet.java @@ -22,17 +22,20 @@ import java.util.Collection; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Objects; import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; +import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.Stream; import static java.util.Collections.unmodifiableCollection; +import static java.util.Collections.unmodifiableSet; import static java.util.Objects.requireNonNull; +import static java.util.stream.Collectors.toCollection; import static java.util.stream.Collectors.toList; -import static java.util.stream.Collectors.toSet; /** * A set containing values that are uniquely identifiable. @@ -63,7 +66,7 @@ public EquatableValueSet( } this.type = type; this.whiteList = whiteList; - this.entries = Collections.unmodifiableSet(new HashSet<>(entries)); + this.entries = unmodifiableSet(new LinkedHashSet<>(entries)); } static EquatableValueSet none(Type type) @@ -78,7 +81,7 @@ static EquatableValueSet all(Type type) static EquatableValueSet of(Type type, Object first, Object... rest) { - HashSet set = new HashSet<>(rest.length + 1); + HashSet set = new LinkedHashSet<>(rest.length + 1); set.add(ValueEntry.create(type, first)); for (Object value : rest) { set.add(ValueEntry.create(type, value)); @@ -90,7 +93,7 @@ static EquatableValueSet copyOf(Type type, Collection values) { return new EquatableValueSet(type, true, values.stream() .map(value -> ValueEntry.create(type, value)) - .collect(toSet())); + .collect(toLinkedSet())); } @JsonProperty @@ -246,20 +249,20 @@ private static Set intersect(Set set1, Set set2) { return set1.stream() .filter(set2::contains) - .collect(toSet()); + .collect(toLinkedSet()); } private static Set union(Set set1, Set set2) { return Stream.concat(set1.stream(), set2.stream()) - .collect(toSet()); + .collect(toLinkedSet()); } private static Set subtract(Set set1, Set set2) { return set1.stream() .filter(value -> !set2.contains(value)) - .collect(toSet()); + .collect(toLinkedSet()); } private EquatableValueSet checkCompatibility(ValueSet other) @@ -294,6 +297,11 @@ public boolean equals(Object obj) && Objects.equals(this.entries, other.entries); } + private static Collector> toLinkedSet() + { + return toCollection(LinkedHashSet::new); + } + public static class ValueEntry { private final Type type; diff --git a/presto-spi/src/main/java/com/facebook/presto/spi/predicate/TupleDomain.java b/presto-spi/src/main/java/com/facebook/presto/spi/predicate/TupleDomain.java index e4410522c190f..dd2d365503927 100644 --- a/presto-spi/src/main/java/com/facebook/presto/spi/predicate/TupleDomain.java +++ b/presto-spi/src/main/java/com/facebook/presto/spi/predicate/TupleDomain.java @@ -25,13 +25,14 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Function; -import java.util.stream.Collectors; +import java.util.stream.Collector; import static java.util.Objects.requireNonNull; import static java.util.stream.Collectors.toList; @@ -97,7 +98,7 @@ public static Optional> extractFixedValues(TupleDomain return Optional.of(tupleDomain.getDomains().get() .entrySet().stream() .filter(entry -> entry.getValue().isNullableSingleValue()) - .collect(toMap(Map.Entry::getKey, entry -> new NullableValue(entry.getValue().getType(), entry.getValue().getNullableSingleValue())))); + .collect(toLinkedMap(Map.Entry::getKey, entry -> new NullableValue(entry.getValue().getType(), entry.getValue().getNullableSingleValue())))); } /** @@ -107,7 +108,7 @@ public static Optional> extractFixedValues(TupleDomain public static TupleDomain fromFixedValues(Map fixedValues) { return TupleDomain.withColumnDomains(fixedValues.entrySet().stream() - .collect(toMap( + .collect(toLinkedMap( Map.Entry::getKey, entry -> { Type type = entry.getValue().getType(); @@ -124,7 +125,7 @@ public static TupleDomain fromColumnDomains(@JsonProperty("columnDomains" return none(); } return withColumnDomains(columnDomains.get().stream() - .collect(toMap(ColumnDomain::getColumn, ColumnDomain::getDomain))); + .collect(toLinkedMap(ColumnDomain::getColumn, ColumnDomain::getDomain))); } @JsonProperty @@ -145,7 +146,7 @@ private static Map normalizeAndCopy(Map domains) { return domains.entrySet().stream() .filter(entry -> !entry.getValue().isAll()) - .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); + .collect(toLinkedMap(Map.Entry::getKey, Map.Entry::getValue)); } /** @@ -187,7 +188,7 @@ public TupleDomain intersect(TupleDomain other) return none(); } - Map intersected = new HashMap<>(this.getDomains().get()); + Map intersected = new LinkedHashMap<>(this.getDomains().get()); for (Map.Entry entry : other.getDomains().get().entrySet()) { Domain intersectionDomain = intersected.get(entry.getKey()); if (intersectionDomain == null) { @@ -266,7 +267,7 @@ public static TupleDomain columnWiseUnion(List> tupleDomai } // group domains by column (only for common columns) - Map> domainsByColumn = new HashMap<>(tupleDomains.size()); + Map> domainsByColumn = new LinkedHashMap<>(tupleDomains.size()); for (TupleDomain domain : tupleDomains) { if (!domain.isNone()) { @@ -284,7 +285,7 @@ public static TupleDomain columnWiseUnion(List> tupleDomai } // finally, do the column-wise union - Map result = new HashMap<>(domainsByColumn.size()); + Map result = new LinkedHashMap<>(domainsByColumn.size()); for (Map.Entry> entry : domainsByColumn.entrySet()) { result.put(entry.getKey(), Domain.union(entry.getValue())); } @@ -351,7 +352,7 @@ else if (isNone()) { } else { buffer.append(domains.get().entrySet().stream() - .collect(toMap(Map.Entry::getKey, entry -> entry.getValue().toString(session)))); + .collect(toLinkedMap(Map.Entry::getKey, entry -> entry.getValue().toString(session)))); } return buffer.toString(); } @@ -362,7 +363,7 @@ public TupleDomain transform(Function function) return TupleDomain.none(); } - HashMap result = new HashMap<>(domains.get().size()); + HashMap result = new LinkedHashMap<>(domains.get().size()); for (Map.Entry entry : domains.get().entrySet()) { U key = function.apply(entry.getKey()); @@ -387,11 +388,20 @@ public TupleDomain simplify() } Map simplified = domains.get().entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().simplify())); + .collect(toLinkedMap(Map.Entry::getKey, e -> e.getValue().simplify())); return TupleDomain.withColumnDomains(simplified); } + private static Collector> toLinkedMap(Function keyMapper, Function valueMapper) + { + return toMap( + keyMapper, + valueMapper, + (u, v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); }, + LinkedHashMap::new); + } + // Available for Jackson serialization only! public static class ColumnDomain { diff --git a/presto-spi/src/main/java/com/facebook/presto/spi/statistics/TableStatistics.java b/presto-spi/src/main/java/com/facebook/presto/spi/statistics/TableStatistics.java index af75c0fd46311..e6f6d37f55f4b 100644 --- a/presto-spi/src/main/java/com/facebook/presto/spi/statistics/TableStatistics.java +++ b/presto-spi/src/main/java/com/facebook/presto/spi/statistics/TableStatistics.java @@ -16,7 +16,7 @@ import com.facebook.presto.spi.ColumnHandle; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; @@ -92,7 +92,7 @@ public static Builder builder() public static final class Builder { private Estimate rowCount = Estimate.unknown(); - private Map columnStatisticsMap = new HashMap<>(); + private Map columnStatisticsMap = new LinkedHashMap<>(); public Builder setRowCount(Estimate rowCount) { diff --git a/presto-spi/src/main/java/com/facebook/presto/spi/statistics/TableStatisticsMetadata.java b/presto-spi/src/main/java/com/facebook/presto/spi/statistics/TableStatisticsMetadata.java index 79d098b487604..6b6d438fe4d6b 100644 --- a/presto-spi/src/main/java/com/facebook/presto/spi/statistics/TableStatisticsMetadata.java +++ b/presto-spi/src/main/java/com/facebook/presto/spi/statistics/TableStatisticsMetadata.java @@ -14,7 +14,7 @@ package com.facebook.presto.spi.statistics; import java.util.ArrayList; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -42,8 +42,8 @@ public TableStatisticsMetadata( Set tableStatistics, List groupingColumns) { - this.columnStatistics = unmodifiableSet(new HashSet<>(requireNonNull(columnStatistics, "columnStatistics is null"))); - this.tableStatistics = unmodifiableSet(new HashSet<>(requireNonNull(tableStatistics, "tableStatistics is null"))); + this.columnStatistics = unmodifiableSet(new LinkedHashSet<>(requireNonNull(columnStatistics, "columnStatistics is null"))); + this.tableStatistics = unmodifiableSet(new LinkedHashSet<>(requireNonNull(tableStatistics, "tableStatistics is null"))); this.groupingColumns = unmodifiableList(new ArrayList<>(requireNonNull(groupingColumns, "groupingColumns is null"))); }