Skip to content
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

Add IntGrouper to avoid unnecessary boxing/unboxing in array-based aggregation #4668

Merged
merged 4 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -266,6 +266,29 @@ private void setupQueries()

basicQueries.put("filter", queryA);
}

{ // basic.singleZipf
final QuerySegmentSpec intervalSpec = new MultipleIntervalSegmentSpec(
Collections.singletonList(basicSchema.getDataInterval())
);
// Use multiple aggregators to see how the number of aggregators impact to the query performance
List<AggregatorFactory> queryAggs = ImmutableList.of(
new LongSumAggregatorFactory("sumLongSequential", "sumLongSequential"),
new LongSumAggregatorFactory("rows", "rows"),
new DoubleSumAggregatorFactory("sumFloatNormal", "sumFloatNormal"),
new DoubleMinAggregatorFactory("minFloatZipf", "minFloatZipf")
);
GroupByQuery queryA = GroupByQuery
.builder()
.setDataSource("blah")
.setQuerySegmentSpec(intervalSpec)
.setDimensions(ImmutableList.of(new DefaultDimensionSpec("dimZipf", null)))
.setAggregatorSpecs(queryAggs)
.setGranularity(Granularity.fromString(queryGranularity))
.build();

basicQueries.put("singleZipf", queryA);
}
SCHEMA_QUERY_MAP.put("basic", basicQueries);

// simple one column schema, for testing performance difference between querying on numeric values as Strings and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.ToIntFunction;

/**
* A buffer grouper for array-based aggregation. This grouper stores aggregated values in the buffer using the grouping
Expand All @@ -48,7 +47,7 @@
* different segments cannot be currently retrieved, this grouper can be used only when performing per-segment query
* execution.
*/
public class BufferArrayGrouper implements Grouper<Integer>
public class BufferArrayGrouper implements IntGrouper
{
private static final Logger LOG = new Logger(BufferArrayGrouper.class);

Expand Down Expand Up @@ -137,16 +136,14 @@ public boolean isInitialized()
}

@Override
public AggregateResult aggregate(Integer key, int dimIndex)
public AggregateResult aggregate(int key, int dimIndex)
{
Preconditions.checkArgument(
dimIndex >= 0 && dimIndex < cardinalityWithMissingValue,
"Invalid dimIndex[%s]",
dimIndex
);

Preconditions.checkNotNull(key);

final int recordOffset = dimIndex * recordSize;

if (recordOffset + recordSize > valBuffer.capacity()) {
Expand Down Expand Up @@ -209,7 +206,7 @@ public void reset()
}

@Override
public ToIntFunction<Integer> hashFunction()
public IntGrouperHashFunction hashFunction()
{
return key -> key + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public ArrayAggregateIterator(
}

@Override
protected Grouper<Integer> newGrouper()
protected IntGrouper newGrouper()
{
return new BufferArrayGrouper(
Suppliers.ofInstance(buffer),
Expand All @@ -595,6 +595,17 @@ protected Grouper<Integer> newGrouper()

@Override
protected void aggregateSingleValueDims(Grouper<Integer> grouper)
{
aggregateSingleValueDims((IntGrouper) grouper);
}

@Override
protected void aggregateMultiValueDims(Grouper<Integer> grouper)
{
aggregateMultiValueDims((IntGrouper) grouper);
}

private void aggregateSingleValueDims(IntGrouper grouper)
{
while (!cursor.isDone()) {
final int key;
Expand All @@ -612,8 +623,7 @@ protected void aggregateSingleValueDims(Grouper<Integer> grouper)
}
}

@Override
protected void aggregateMultiValueDims(Grouper<Integer> grouper)
private void aggregateMultiValueDims(IntGrouper grouper)
{
if (dim == null) {
throw new ISE("dim must exist");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.druid.query.groupby.epinephelinae;

import com.google.common.base.Preconditions;

import java.util.function.ToIntFunction;

/**
* {@link Grouper} specialized for the primitive int type
*/
public interface IntGrouper extends Grouper<Integer>
Copy link
Member

Choose a reason for hiding this comment

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

Since this interface have only one implementation, maybe get rid of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm thinking to add a new grouper which doing merge aggregation for sorted data. This interface will be worthwhile.

{
default AggregateResult aggregate(int key)
Copy link
Member

Choose a reason for hiding this comment

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

Seems that this default method is used only from other default method, suggested to inline it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method is used in ArrayAggregateIterator as well.

{
return aggregate(key, hashFunction().apply(key));
}

AggregateResult aggregate(int key, int keyHash);

/**
* {@inheritDoc}
*
* @deprecated Please use {@link #aggregate(int)} instead.
*/
@Deprecated
@Override
default AggregateResult aggregate(Integer key)
{
Preconditions.checkNotNull(key);
return aggregate(key.intValue());
}

/**
* {@inheritDoc}
*
* @deprecated Please use {@link #aggregate(int, int)} instead.
*/
@Deprecated
@Override
default AggregateResult aggregate(Integer key, int keyHash)
{
Preconditions.checkNotNull(key);
return aggregate(key.intValue(), keyHash);
}

@Override
IntGrouperHashFunction hashFunction();

interface IntGrouperHashFunction extends ToIntFunction<Integer>
{
@Override
default int applyAsInt(Integer value)
{
return apply(value.intValue());
}

int apply(int value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class BufferArrayGrouperTest
public void testAggregate()
{
final TestColumnSelectorFactory columnSelectorFactory = GrouperTestUtil.newColumnSelectorFactory();
final Grouper<Integer> grouper = newGrouper(columnSelectorFactory, 1024);
final IntGrouper grouper = newGrouper(columnSelectorFactory, 1024);

columnSelectorFactory.setRow(new MapBasedRow(0, ImmutableMap.of("value", 10L)));
grouper.aggregate(12);
Expand Down