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

backport 3471 #3540

Merged
merged 1 commit into from
Oct 5, 2016
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 @@ -107,6 +107,8 @@ static void updateUnion(Union union, Object update)
union.update((Memory) update);
} else if (update instanceof Sketch) {
union.update((Sketch) update);
} else if (update instanceof Union) {
union.update(((Union) update).getResult(false, null));
} else if (update instanceof String) {
union.update((String) update);
} else if (update instanceof byte[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,21 @@ public Comparator<Sketch> getComparator()
@Override
public Object combine(Object lhs, Object rhs)
{
Union union = (Union) SetOperation.builder().build(size, Family.UNION);
updateUnion(union, lhs);
updateUnion(union, rhs);
return union.getResult(false, null);
final Union union;
if (lhs instanceof Union) {
union = (Union) lhs;
updateUnion(union, rhs);
} else if (rhs instanceof Union) {
union = (Union) rhs;
updateUnion(union, lhs);
} else {
union = (Union) SetOperation.builder().build(size, Family.UNION);
updateUnion(union, lhs);
updateUnion(union, rhs);
}


return union;
}

private void updateUnion(Union union, Object obj)
Expand All @@ -124,6 +135,8 @@ private void updateUnion(Union union, Object obj)
union.update((Memory) obj);
} else if (obj instanceof Sketch) {
union.update((Sketch) obj);
} else if (obj instanceof Union) {
union.update(((Union) obj).getResult(false, null));
} else {
throw new IAE("Object of type [%s] can not be unioned", obj.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public int compare(Object o1, Object o2)
@Override
public Object compute(Map<String, Object> combinedAggregators)
{
Sketch sketch = (Sketch) field.compute(combinedAggregators);
Sketch sketch = SketchSetPostAggregator.toSketch(field.compute(combinedAggregators));
if (errorBoundsStdDev != null) {
SketchEstimateWithErrorBounds result = new SketchEstimateWithErrorBounds(
sketch.getEstimate(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.inject.Binder;
import com.yahoo.sketches.memory.Memory;
import com.yahoo.sketches.theta.Sketch;
import com.yahoo.sketches.theta.Union;
import io.druid.initialization.DruidModule;
import io.druid.segment.serde.ComplexMetrics;

Expand Down Expand Up @@ -71,7 +72,11 @@ public List<? extends Module> getJacksonModules()
Sketch.class, new SketchJsonSerializer()
)
.addSerializer(
Memory.class, new MemoryJsonSerializer())
Memory.class, new MemoryJsonSerializer()
)
.addSerializer(
Union.class, new UnionJsonSerializer()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.yahoo.sketches.memory.NativeMemory;
import com.yahoo.sketches.theta.Sketch;
import com.yahoo.sketches.theta.Sketches;
import com.yahoo.sketches.theta.Union;
import io.druid.segment.data.ObjectStrategy;

import java.nio.ByteBuffer;
Expand Down Expand Up @@ -98,6 +99,8 @@ public byte[] toBytes(Object obj)
byte[] retVal = new byte[(int) mem.getCapacity()];
mem.getByteArray(0, retVal, 0, (int) mem.getCapacity());
return retVal;
} else if (obj instanceof Union) {
return toBytes(((Union) obj).getResult(true, null));
} else if (obj == null) {
return EMPTY_BYTES;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.metamx.common.logger.Logger;
import com.yahoo.sketches.Util;
import com.yahoo.sketches.theta.Sketch;
import com.yahoo.sketches.theta.Union;
import io.druid.query.aggregation.PostAggregator;

import java.util.Comparator;
Expand Down Expand Up @@ -83,12 +84,23 @@ public Object compute(final Map<String, Object> combinedAggregators)
{
Sketch[] sketches = new Sketch[fields.size()];
for (int i = 0; i < sketches.length; i++) {
sketches[i] = (Sketch) fields.get(i).compute(combinedAggregators);
sketches[i] = toSketch(fields.get(i).compute(combinedAggregators));
}

return SketchOperations.sketchSetOperation(func, maxSketchSize, sketches);
}

public final static Sketch toSketch(Object obj)
{
if (obj instanceof Sketch) {
return (Sketch) obj;
} else if (obj instanceof Union) {
return ((Union) obj).getResult(true, null);
} else {
throw new IAE("Can't convert to Sketch object [%s]", obj.getClass());
}
}

@Override
@JsonProperty
public String getName()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.aggregation.datasketches.theta;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.yahoo.sketches.theta.Union;

import java.io.IOException;

/**
*/
public class UnionJsonSerializer extends JsonSerializer<Union>
{
@Override
public void serialize(Union union, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException
{
jgen.writeBinary(union.getResult(true, null).toByteArray());
}
}