private ListMultimap<Symbol, Symbol> canonicalizeSetOperationSymbolMap(ListMultimap<Symbol, Symbol> setOperationSymbolMap) { ImmutableListMultimap.Builder<Symbol, Symbol> builder = ImmutableListMultimap.builder(); Set<Symbol> addedSymbols = new HashSet<>(); for (Map.Entry<Symbol, Collection<Symbol>> entry : setOperationSymbolMap.asMap().entrySet()) { Symbol canonicalOutputSymbol = canonicalize(entry.getKey()); if (addedSymbols.add(canonicalOutputSymbol)) { builder.putAll(canonicalOutputSymbol, Iterables.transform(entry.getValue(), this::canonicalize)); } } return builder.build(); } }
public FunctionMap(FunctionMap map, Iterable<? extends SqlFunction> functions) { this.functions = ImmutableListMultimap.<QualifiedName, SqlFunction>builder() .putAll(map.functions) .putAll(Multimaps.index(functions, function -> QualifiedName.of(function.getSignature().getName()))) .build(); // Make sure all functions with the same name are aggregations or none of them are for (Map.Entry<QualifiedName, Collection<SqlFunction>> entry : this.functions.asMap().entrySet()) { Collection<SqlFunction> values = entry.getValue(); long aggregations = values.stream() .map(function -> function.getSignature().getKind()) .filter(kind -> kind == AGGREGATE) .count(); checkState(aggregations == 0 || aggregations == values.size(), "'%s' is both an aggregation and a scalar function", entry.getKey()); } }
private static void flattenSetOperation(SetOperationNode node, RewriteContext<Boolean> context, ImmutableList.Builder<PlanNode> flattenedSources, ImmutableListMultimap.Builder<Symbol, Symbol> flattenedSymbolMap) { for (int i = 0; i < node.getSources().size(); i++) { PlanNode subplan = node.getSources().get(i); PlanNode rewrittenSource = context.rewrite(subplan, context.get()); Class<?> setOperationClass = node.getClass(); if (setOperationClass.isInstance(rewrittenSource) && (!setOperationClass.equals(ExceptNode.class) || i == 0)) { // Absorb source's subplans if it is also a SetOperation of the same type // ExceptNodes can only flatten their first source because except is not associative SetOperationNode rewrittenSetOperation = (SetOperationNode) rewrittenSource; flattenedSources.addAll(rewrittenSetOperation.getSources()); for (Map.Entry<Symbol, Collection<Symbol>> entry : node.getSymbolMapping().asMap().entrySet()) { Symbol inputSymbol = Iterables.get(entry.getValue(), i); flattenedSymbolMap.putAll(entry.getKey(), rewrittenSetOperation.getSymbolMapping().get(inputSymbol)); } } else { flattenedSources.add(rewrittenSource); for (Map.Entry<Symbol, Collection<Symbol>> entry : node.getSymbolMapping().asMap().entrySet()) { flattenedSymbolMap.put(entry.getKey(), Iterables.get(entry.getValue(), i)); } } } }
public PlanSanityChecker(boolean forceSingleNode) { checkers = ImmutableListMultimap.<Stage, Checker>builder() .putAll( Stage.INTERMEDIATE, new ValidateDependenciesChecker(), new NoDuplicatePlanNodeIdsChecker(), new TypeValidator(), new NoSubqueryExpressionLeftChecker(), new NoIdentifierLeftChecker(), new VerifyOnlyOneOutputNode()) .putAll( Stage.FINAL, new ValidateDependenciesChecker(), new NoDuplicatePlanNodeIdsChecker(), new TypeValidator(), new NoSubqueryExpressionLeftChecker(), new NoIdentifierLeftChecker(), new VerifyOnlyOneOutputNode(), new VerifyNoFilteredAggregations(), new ValidateAggregationsWithDefaultValues(forceSingleNode), new ValidateStreamingAggregations()) .build(); }
final Builder addAllSingularFields(int fieldNumber, Iterable<SingularField> singularFields) { singularFieldsBuilder().putAll(fieldNumber, singularFields); return this; }
ListMultimap<String, String> allMetaservices() { ImmutableListMultimap.Builder<String, String> builder = ImmutableListMultimap.builder(); for (Element element : round().getElementsAnnotatedWith(Metainf.Service.class)) { @Nullable TypeElement typeElement = validated(element); if (typeElement == null) { continue; } Set<String> interfaceNames = extractServiceInterfaceNames(typeElement); String binaryName = processing().getElementUtils().getBinaryName(typeElement).toString(); builder.putAll(binaryName, interfaceNames); } return builder.build(); }
/** * Returns an immutable multimap containing the specified entries. The returned multimap iterates * over keys in the order they were first encountered in the input, and the values for each key * are iterated in the order they were encountered. * * @throws NullPointerException if any key, value, or entry is null * @since 19.0 */ @Beta public static <K, V> ImmutableListMultimap<K, V> copyOf( Iterable<? extends Entry<? extends K, ? extends V>> entries) { return new Builder<K, V>().putAll(entries).build(); }
@Test public void testPushThroughUnion() { tester().assertThat(new PushTableWriteThroughUnion()) .on(p -> p.tableWriter( ImmutableList.of(p.symbol("A", BIGINT), p.symbol("B", BIGINT)), ImmutableList.of("a", "b"), p.union( ImmutableListMultimap.<Symbol, Symbol>builder() .putAll(p.symbol("A", BIGINT), p.symbol("A1", BIGINT), p.symbol("B2", BIGINT)) .putAll(p.symbol("B", BIGINT), p.symbol("B1", BIGINT), p.symbol("A2", BIGINT)) .build(), ImmutableList.of( p.values(p.symbol("A1", BIGINT), p.symbol("B1", BIGINT)), p.values(p.symbol("A2", BIGINT), p.symbol("B2", BIGINT)))))) .matches(union( tableWriter(ImmutableList.of("A1", "B1"), ImmutableList.of("a", "b"), values(ImmutableMap.of("A1", 0, "B1", 1))), tableWriter(ImmutableList.of("B2", "A2"), ImmutableList.of("a", "b"), values(ImmutableMap.of("A2", 0, "B2", 1))))); } }
public void testBuilderPutNullValue() { Multimap<String, Integer> toPut = LinkedListMultimap.create(); toPut.put(null, 1); ImmutableListMultimap.Builder<String, Integer> builder = ImmutableListMultimap.builder(); try { builder.put("foo", null); fail(); } catch (NullPointerException expected) { } try { builder.putAll("foo", Arrays.asList(1, null, 3)); fail(); } catch (NullPointerException expected) { } try { builder.putAll("foo", 1, null, 3); fail(); } catch (NullPointerException expected) { } try { builder.putAll(toPut); fail(); } catch (NullPointerException expected) { } }
public void testBuilderPutNullKey() { Multimap<String, Integer> toPut = LinkedListMultimap.create(); toPut.put("foo", null); ImmutableListMultimap.Builder<String, Integer> builder = ImmutableListMultimap.builder(); try { builder.put(null, 1); fail(); } catch (NullPointerException expected) { } try { builder.putAll(null, Arrays.asList(1, 2, 3)); fail(); } catch (NullPointerException expected) { } try { builder.putAll(null, 1, 2, 3); fail(); } catch (NullPointerException expected) { } try { builder.putAll(toPut); fail(); } catch (NullPointerException expected) { } }
private ListMultimap<Symbol, Symbol> rewriteSetOperationSymbolMapping(SetOperationNode node, RewriteContext<Set<Symbol>> context) { // Find out which output symbols we need to keep ImmutableListMultimap.Builder<Symbol, Symbol> rewrittenSymbolMappingBuilder = ImmutableListMultimap.builder(); for (Symbol symbol : node.getOutputSymbols()) { if (context.get().contains(symbol)) { rewrittenSymbolMappingBuilder.putAll(symbol, node.getSymbolMapping().get(symbol)); } } return rewrittenSymbolMappingBuilder.build(); }
public void testFlatteningToImmutableListMultimap() { Collector<String, ?, ImmutableListMultimap<Character, Character>> collector = ImmutableListMultimap.flatteningToImmutableListMultimap( str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c)); BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence = Equivalence.equals() .onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet())) .and(Equivalence.equals()); ImmutableListMultimap<Character, Character> empty = ImmutableListMultimap.of(); ImmutableListMultimap<Character, Character> filled = ImmutableListMultimap.<Character, Character>builder() .putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a')) .putAll('a', Arrays.asList('p', 'p', 'l', 'e')) .putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't')) .putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's')) .putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y')) .build(); CollectorTester.of(collector, equivalence) .expectCollects(empty) .expectCollects(filled, "banana", "apple", "carrot", "asparagus", "cherry"); }
public void testBuilderPutAllMultimapWithDuplicates() { Multimap<String, Integer> toPut = LinkedListMultimap.create(); toPut.put("foo", 1); toPut.put("bar", 4); toPut.put("foo", 2); toPut.put("foo", 1); toPut.put("bar", 5); Multimap<String, Integer> moreToPut = LinkedListMultimap.create(); moreToPut.put("foo", 6); moreToPut.put("bar", 4); moreToPut.put("foo", 7); moreToPut.put("foo", 2); ImmutableListMultimap.Builder<String, Integer> builder = ImmutableListMultimap.builder(); builder.putAll(toPut); builder.putAll(moreToPut); Multimap<String, Integer> multimap = builder.build(); assertEquals(Arrays.asList(1, 2, 1, 6, 7, 2), multimap.get("foo")); assertEquals(Arrays.asList(4, 5, 4), multimap.get("bar")); assertEquals(9, multimap.size()); }
public void testIndex() { ImmutableListMultimap<Integer, String> expected = ImmutableListMultimap.<Integer, String>builder() .putAll(3, "one", "two") .put(5, "three") .put(4, "four") .build(); ImmutableListMultimap<Integer, String> index = FluentIterable.from(asList("one", "two", "three", "four")) .index( new Function<String, Integer>() { @Override public Integer apply(String input) { return input.length(); } }); assertEquals(expected, index); }
public void testBuilderPutAllMultimap() { Multimap<String, Integer> toPut = LinkedListMultimap.create(); toPut.put("foo", 1); toPut.put("bar", 4); toPut.put("foo", 2); toPut.put("foo", 3); Multimap<String, Integer> moreToPut = LinkedListMultimap.create(); moreToPut.put("foo", 6); moreToPut.put("bar", 5); moreToPut.put("foo", 7); ImmutableListMultimap.Builder<String, Integer> builder = ImmutableListMultimap.builder(); builder.putAll(toPut); builder.putAll(moreToPut); Multimap<String, Integer> multimap = builder.build(); assertEquals(Arrays.asList(1, 2, 3, 6, 7), multimap.get("foo")); assertEquals(Arrays.asList(4, 5), multimap.get("bar")); assertEquals(7, multimap.size()); }
/** * Returns an immutable multimap containing the specified entries. The returned multimap iterates * over keys in the order they were first encountered in the input, and the values for each key * are iterated in the order they were encountered. * * @throws NullPointerException if any key, value, or entry is null * @since 19.0 */ @Beta public static <K, V> ImmutableListMultimap<K, V> copyOf( Iterable<? extends Entry<? extends K, ? extends V>> entries) { return new Builder<K, V>().putAll(entries).build(); }
public void testBuilderPutAllVarargs() { ImmutableListMultimap.Builder<String, Integer> builder = ImmutableListMultimap.builder(); builder.putAll("foo", 1, 2, 3); builder.putAll("bar", 4, 5); builder.putAll("foo", 6, 7); Multimap<String, Integer> multimap = builder.build(); assertEquals(Arrays.asList(1, 2, 3, 6, 7), multimap.get("foo")); assertEquals(Arrays.asList(4, 5), multimap.get("bar")); assertEquals(7, multimap.size()); }
public void testBuilderPutAllWithDuplicates() { ImmutableListMultimap.Builder<String, Integer> builder = ImmutableListMultimap.builder(); builder.putAll("foo", 1, 2, 3); builder.putAll("bar", 4, 5); builder.putAll("foo", 1, 6, 7); ImmutableListMultimap<String, Integer> multimap = builder.build(); assertEquals(Arrays.asList(1, 2, 3, 1, 6, 7), multimap.get("foo")); assertEquals(Arrays.asList(4, 5), multimap.get("bar")); assertEquals(8, multimap.size()); }
public void testBuilderPutWithDuplicates() { ImmutableListMultimap.Builder<String, Integer> builder = ImmutableListMultimap.builder(); builder.putAll("foo", 1, 2, 3); builder.putAll("bar", 4, 5); builder.put("foo", 1); ImmutableListMultimap<String, Integer> multimap = builder.build(); assertEquals(Arrays.asList(1, 2, 3, 1), multimap.get("foo")); assertEquals(Arrays.asList(4, 5), multimap.get("bar")); assertEquals(6, multimap.size()); }
public void testBuilderPutAllIterable() { ImmutableListMultimap.Builder<String, Integer> builder = ImmutableListMultimap.builder(); builder.putAll("foo", Arrays.asList(1, 2, 3)); builder.putAll("bar", Arrays.asList(4, 5)); builder.putAll("foo", Arrays.asList(6, 7)); Multimap<String, Integer> multimap = builder.build(); assertEquals(Arrays.asList(1, 2, 3, 6, 7), multimap.get("foo")); assertEquals(Arrays.asList(4, 5), multimap.get("bar")); assertEquals(7, multimap.size()); }