@Test public void testArrayHistograms() throws Exception { ArrayType arrayType = new ArrayType(VARCHAR); MapType mapType = new MapType(arrayType, BIGINT); InternalAggregationFunction aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), arrayType.getTypeSignature().toString())); assertAggregation( aggregationFunction, 1.0, ImmutableMap.of(ImmutableList.of("a", "b", "c"), 1L, ImmutableList.of("d", "e", "f"), 1L, ImmutableList.of("c", "b", "a"), 1L), createStringArraysBlock(ImmutableList.of(ImmutableList.of("a", "b", "c"), ImmutableList.of("d", "e", "f"), ImmutableList.of("c", "b", "a")))); }
private static <K, V> void testMultimapAgg(Type keyType, List<K> expectedKeys, Type valueType, List<V> expectedValues) { checkState(expectedKeys.size() == expectedValues.size(), "expectedKeys and expectedValues should have equal size"); MapType mapType = new MapType(keyType, new ArrayType(valueType)); Signature signature = new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), keyType.getTypeSignature(), valueType.getTypeSignature()); InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(signature); Map<K, List<V>> map = new HashMap<>(); for (int i = 0; i < expectedKeys.size(); i++) { if (!map.containsKey(expectedKeys.get(i))) { map.put(expectedKeys.get(i), new ArrayList<>()); } map.get(expectedKeys.get(i)).add(expectedValues.get(i)); } RowPageBuilder builder = RowPageBuilder.rowPageBuilder(keyType, valueType); for (int i = 0; i < expectedKeys.size(); i++) { builder.row(expectedKeys.get(i), expectedValues.get(i)); } assertAggregation(aggFunc, 1.0, map.isEmpty() ? null : map, builder.build().getBlocks()); } }
@Test public void testDoubleArrayMap() throws Exception { ArrayType arrayType = new ArrayType(VARCHAR); MapType mapType = new MapType(DOUBLE, arrayType); InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.DOUBLE, arrayType.getTypeSignature().toString())); assertAggregation( aggFunc, 1.0, ImmutableMap.of(1.0, ImmutableList.of("a", "b"), 2.0, ImmutableList.of("c", "d"), 3.0, ImmutableList.of("e", "f")), createDoublesBlock(1.0, 2.0, 3.0), createStringArraysBlock(ImmutableList.of(ImmutableList.of("a", "b"), ImmutableList.of("c", "d"), ImmutableList.of("e", "f")))); }
@Test public void testArrayDoubleMap() throws Exception { ArrayType arrayType = new ArrayType(VARCHAR); MapType mapType = new MapType(arrayType, DOUBLE); InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature( NAME, AGGREGATE, mapType.getTypeSignature().toString(), arrayType.getTypeSignature().toString(), StandardTypes.DOUBLE )); assertAggregation( aggFunc, 1.0, ImmutableMap.of( ImmutableList.of("a", "b"), 1.0, ImmutableList.of("c", "d"), 2.0, ImmutableList.of("e", "f"), 3.0), createStringArraysBlock(ImmutableList.of(ImmutableList.of("a", "b"), ImmutableList.of("c", "d"), ImmutableList.of("e", "f"))), createDoublesBlock(1.0, 2.0, 3.0)); } }
@Test public void testLargerHistograms() throws Exception { MapType mapType = new MapType(VARCHAR, BIGINT); InternalAggregationFunction aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.VARCHAR)); assertAggregation( aggregationFunction, 1.0, ImmutableMap.of("a", 25L, "b", 10L, "c", 12L, "d", 1L, "e", 2L), createStringsBlock("a", "b", "c", "d", "e", "e", "c", "a", "a", "a", "b", "a", "a", "a", "a", "b", "a", "a", "a", "a", "b", "a", "a", "a", "a", "b", "a", "a", "a", "a", "b", "a", "c", "c", "b", "a", "c", "c", "b", "a", "c", "c", "b", "a", "c", "c", "b", "a", "c", "c")); } }
@Test public void testMapHistograms() throws Exception { MapType innerMapType = new MapType(VARCHAR, VARCHAR); MapType mapType = new MapType(innerMapType, BIGINT); InternalAggregationFunction aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), innerMapType.getTypeSignature().toString())); BlockBuilder builder = innerMapType.createBlockBuilder(new BlockBuilderStatus(), 3); innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("a", "b"))); innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("c", "d"))); innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("e", "f"))); assertAggregation( aggregationFunction, 1.0, ImmutableMap.of(ImmutableMap.of("a", "b"), 1L, ImmutableMap.of("c", "d"), 1L, ImmutableMap.of("e", "f"), 1L), builder.build()); }
@Test public void testDoubleMapMap() throws Exception { MapType innerMapType = new MapType(VARCHAR, VARCHAR); MapType mapType = new MapType(DOUBLE, innerMapType); InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.DOUBLE, innerMapType.getTypeSignature().toString())); BlockBuilder builder = innerMapType.createBlockBuilder(new BlockBuilderStatus(), 3); innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("a", "b"))); innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("c", "d"))); innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("e", "f"))); assertAggregation( aggFunc, 1.0, ImmutableMap.of(1.0, ImmutableMap.of("a", "b"), 2.0, ImmutableMap.of("c", "d"), 3.0, ImmutableMap.of("e", "f")), createDoublesBlock(1.0, 2.0, 3.0), builder.build()); }
@Test public void testDuplicateKeysValues() throws Exception { MapType mapType = new MapType(VARCHAR, BIGINT); InternalAggregationFunction aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.VARCHAR)); assertAggregation( aggregationFunction, 1.0, ImmutableMap.of("a", 2L, "b", 1L), createStringsBlock("a", "b", "a")); mapType = new MapType(TIMESTAMP_WITH_TIME_ZONE, BIGINT); aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.TIMESTAMP_WITH_TIME_ZONE)); long timestampWithTimeZone1 = packDateTimeWithZone(new DateTime(1970, 1, 1, 0, 0, 0, 0, DATE_TIME_ZONE).getMillis(), TIME_ZONE_KEY); long timestampWithTimeZone2 = packDateTimeWithZone(new DateTime(2015, 1, 1, 0, 0, 0, 0, DATE_TIME_ZONE).getMillis(), TIME_ZONE_KEY); assertAggregation( aggregationFunction, 1.0, ImmutableMap.of(new SqlTimestampWithTimeZone(timestampWithTimeZone1), 2L, new SqlTimestampWithTimeZone(timestampWithTimeZone2), 1L), createLongsBlock(timestampWithTimeZone1, timestampWithTimeZone1, timestampWithTimeZone2)); }
@Test public void testRowHistograms() throws Exception { RowType innerRowType = new RowType(ImmutableList.of(BIGINT, DOUBLE), Optional.of(ImmutableList.of("f1", "f2"))); MapType mapType = new MapType(innerRowType, BIGINT); InternalAggregationFunction aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), innerRowType.getTypeSignature().toString())); BlockBuilder builder = innerRowType.createBlockBuilder(new BlockBuilderStatus(), 3); innerRowType.writeObject(builder, testRowBigintBigint(1L, 1.0)); innerRowType.writeObject(builder, testRowBigintBigint(2L, 2.0)); innerRowType.writeObject(builder, testRowBigintBigint(3L, 3.0)); assertAggregation( aggregationFunction, 1.0, ImmutableMap.of(ImmutableList.of(1L, 1.0), 1L, ImmutableList.of(2L, 2.0), 1L, ImmutableList.of(3L, 3.0), 1L), builder.build()); }
@Test public void testWithNulls() throws Exception { MapType mapType = new MapType(BIGINT, BIGINT); InternalAggregationFunction aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.BIGINT)); assertAggregation( aggregationFunction, 1.0, ImmutableMap.of(1L, 1L, 2L, 1L), createLongsBlock(2L, null, 1L)); mapType = new MapType(BIGINT, BIGINT); aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.BIGINT)); assertAggregation( aggregationFunction, 1.0, null, createLongsBlock((Long) null)); }
@Test public void testDoubleRowMap() throws Exception { RowType innerRowType = new RowType(ImmutableList.of(BIGINT, DOUBLE), Optional.of(ImmutableList.of("f1", "f2"))); MapType mapType = new MapType(DOUBLE, innerRowType); InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.DOUBLE, innerRowType.getTypeSignature().toString())); BlockBuilder builder = innerRowType.createBlockBuilder(new BlockBuilderStatus(), 3); innerRowType.writeObject(builder, testRowBigintBigint(1L, 1.0)); innerRowType.writeObject(builder, testRowBigintBigint(2L, 2.0)); innerRowType.writeObject(builder, testRowBigintBigint(3L, 3.0)); assertAggregation( aggFunc, 1.0, ImmutableMap.of(1.0, ImmutableList.of(1L, 1.0), 2.0, ImmutableList.of(2L, 2.0), 3.0, ImmutableList.of(3L, 3.0)), createDoublesBlock(1.0, 2.0, 3.0), builder.build()); }
@Test public void testDuplicateKeysValues() throws Exception { MapType mapType = new MapType(DOUBLE, VARCHAR); InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.DOUBLE, StandardTypes.VARCHAR)); assertAggregation( aggFunc, 1.0, ImmutableMap.of(1.0, "a"), createDoublesBlock(1.0, 1.0, 1.0), createStringsBlock("a", "b", "c")); mapType = new MapType(DOUBLE, BIGINT); aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.DOUBLE, StandardTypes.BIGINT)); assertAggregation( aggFunc, 1.0, ImmutableMap.of(1.0, 99L, 2.0, 99L, 3.0, 99L), createDoublesBlock(1.0, 2.0, 3.0), createLongsBlock(99L, 99L, 99L)); }
@Test public void testNull() throws Exception { InternalAggregationFunction doubleDouble = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, new MapType(DOUBLE, DOUBLE).getTypeSignature().toString(), StandardTypes.DOUBLE, StandardTypes.DOUBLE)); assertAggregation( doubleDouble, 1.0, ImmutableMap.of(1.0, 2.0), createDoublesBlock(1.0, null, null), createDoublesBlock(2.0, 3.0, 4.0)); assertAggregation( doubleDouble, 1.0, null, createDoublesBlock(null, null, null), createDoublesBlock(2.0, 3.0, 4.0)); Map<Double, Double> expected = new LinkedHashMap<>(); expected.put(1.0, null); expected.put(2.0, null); expected.put(3.0, null); assertAggregation( doubleDouble, 1.0, expected, createDoublesBlock(1.0, 2.0, 3.0), createDoublesBlock(null, null, null)); }
InternalAggregationFunction aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.VARCHAR)); assertAggregation( aggregationFunction, aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.BIGINT)); assertAggregation( aggregationFunction, aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.DOUBLE)); assertAggregation( aggregationFunction, aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.BOOLEAN)); assertAggregation( aggregationFunction,
@Test public void testSimpleMaps() throws Exception { MapType mapType = new MapType(DOUBLE, VARCHAR); InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.DOUBLE, StandardTypes.VARCHAR)); assertAggregation( aggFunc, 1.0, ImmutableMap.of(1.0, "a", 2.0, "b", 3.0, "c"), createDoublesBlock(1.0, 2.0, 3.0), createStringsBlock("a", "b", "c")); mapType = new MapType(DOUBLE, BIGINT); aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.DOUBLE, StandardTypes.BIGINT)); assertAggregation( aggFunc, 1.0, ImmutableMap.of(1.0, 3L, 2.0, 2L, 3.0, 1L), createDoublesBlock(1.0, 2.0, 3.0), createLongsBlock(3L, 2L, 1L)); mapType = new MapType(DOUBLE, BOOLEAN); aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature().toString(), StandardTypes.DOUBLE, StandardTypes.BOOLEAN)); assertAggregation( aggFunc, 1.0, ImmutableMap.of(1.0, true, 2.0, false, 3.0, false), createDoublesBlock(1.0, 2.0, 3.0), createBooleansBlock(true, false, false)); }