Search in sources :

Example 11 with Comparator

use of java.util.Comparator in project druid by druid-io.

the class DoubleSumAggregatorTest method testComparator.

@Test
public void testComparator() {
    final TestFloatColumnSelector selector = new TestFloatColumnSelector(new float[] { 0.15f, 0.27f });
    DoubleSumAggregator agg = new DoubleSumAggregator(selector);
    Object first = agg.get();
    agg.aggregate();
    Comparator comp = new DoubleSumAggregatorFactory("null", "null").getComparator();
    Assert.assertEquals(-1, comp.compare(first, agg.get()));
    Assert.assertEquals(0, comp.compare(first, first));
    Assert.assertEquals(0, comp.compare(agg.get(), agg.get()));
    Assert.assertEquals(1, comp.compare(agg.get(), first));
}
Also used : Comparator(java.util.Comparator) Test(org.junit.Test)

Example 12 with Comparator

use of java.util.Comparator in project druid by druid-io.

the class LongLeastPostAggregatorTest method testComparator.

@Test
public void testComparator() {
    final String aggName = "rows";
    LongLeastPostAggregator leastPostAggregator;
    CountAggregator agg = new CountAggregator();
    Map<String, Object> metricValues = new HashMap<String, Object>();
    metricValues.put(aggName, agg.get());
    List<PostAggregator> postAggregatorList = Lists.newArrayList(new ConstantPostAggregator("roku", 2D), new FieldAccessPostAggregator("rows", aggName));
    leastPostAggregator = new LongLeastPostAggregator("leastPostAggregator", postAggregatorList);
    Comparator comp = leastPostAggregator.getComparator();
    Object before = leastPostAggregator.compute(metricValues);
    agg.aggregate();
    agg.aggregate();
    agg.aggregate();
    metricValues.put(aggName, agg.get());
    Object after = leastPostAggregator.compute(metricValues);
    Assert.assertEquals(-1, comp.compare(before, after));
    Assert.assertEquals(0, comp.compare(before, before));
    Assert.assertEquals(0, comp.compare(after, after));
    Assert.assertEquals(1, comp.compare(after, before));
}
Also used : PostAggregator(io.druid.query.aggregation.PostAggregator) HashMap(java.util.HashMap) CountAggregator(io.druid.query.aggregation.CountAggregator) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 13 with Comparator

use of java.util.Comparator in project druid by druid-io.

the class HyperUniquesAggregatorFactoryTest method testCompare2.

@Test
public void testCompare2() throws Exception {
    Random rand = new Random(0);
    HyperUniquesAggregatorFactory factory = new HyperUniquesAggregatorFactory("foo", "bar");
    Comparator comparator = factory.getComparator();
    for (int i = 1; i < 1000; ++i) {
        HyperLogLogCollector collector1 = HyperLogLogCollector.makeLatestCollector();
        int j = rand.nextInt(50);
        for (int l = 0; l < j; ++l) {
            collector1.add(fn.hashLong(rand.nextLong()).asBytes());
        }
        HyperLogLogCollector collector2 = HyperLogLogCollector.makeLatestCollector();
        int k = j + 1 + rand.nextInt(5);
        for (int l = 0; l < k; ++l) {
            collector2.add(fn.hashLong(rand.nextLong()).asBytes());
        }
        Assert.assertEquals(Double.compare(collector1.estimateCardinality(), collector2.estimateCardinality()), comparator.compare(collector1, collector2));
    }
    for (int i = 1; i < 100; ++i) {
        HyperLogLogCollector collector1 = HyperLogLogCollector.makeLatestCollector();
        int j = rand.nextInt(500);
        for (int l = 0; l < j; ++l) {
            collector1.add(fn.hashLong(rand.nextLong()).asBytes());
        }
        HyperLogLogCollector collector2 = HyperLogLogCollector.makeLatestCollector();
        int k = j + 2 + rand.nextInt(5);
        for (int l = 0; l < k; ++l) {
            collector2.add(fn.hashLong(rand.nextLong()).asBytes());
        }
        Assert.assertEquals(Double.compare(collector1.estimateCardinality(), collector2.estimateCardinality()), comparator.compare(collector1, collector2));
    }
    for (int i = 1; i < 10; ++i) {
        HyperLogLogCollector collector1 = HyperLogLogCollector.makeLatestCollector();
        int j = rand.nextInt(100000);
        for (int l = 0; l < j; ++l) {
            collector1.add(fn.hashLong(rand.nextLong()).asBytes());
        }
        HyperLogLogCollector collector2 = HyperLogLogCollector.makeLatestCollector();
        int k = j + 20000 + rand.nextInt(100000);
        for (int l = 0; l < k; ++l) {
            collector2.add(fn.hashLong(rand.nextLong()).asBytes());
        }
        Assert.assertEquals(Double.compare(collector1.estimateCardinality(), collector2.estimateCardinality()), comparator.compare(collector1, collector2));
    }
}
Also used : Random(java.util.Random) HyperLogLogCollector(io.druid.hll.HyperLogLogCollector) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 14 with Comparator

use of java.util.Comparator in project druid by druid-io.

the class HyperUniquesAggregatorFactoryTest method testCompare1.

@Test
public void testCompare1() throws Exception {
    HyperLogLogCollector collector1 = HyperLogLogCollector.makeLatestCollector();
    HyperLogLogCollector collector2 = HyperLogLogCollector.makeLatestCollector();
    collector1.add(fn.hashLong(0).asBytes());
    HyperUniquesAggregatorFactory factory = new HyperUniquesAggregatorFactory("foo", "bar");
    Comparator comparator = factory.getComparator();
    for (int i = 1; i < 100; i = i + 2) {
        collector1.add(fn.hashLong(i).asBytes());
        collector2.add(fn.hashLong(i + 1).asBytes());
        Assert.assertEquals(1, comparator.compare(collector1, collector2));
        Assert.assertEquals(1, Double.compare(collector1.estimateCardinality(), collector2.estimateCardinality()));
    }
}
Also used : HyperLogLogCollector(io.druid.hll.HyperLogLogCollector) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 15 with Comparator

use of java.util.Comparator in project druid by druid-io.

the class HyperUniquesAggregatorFactoryTest method testCompareToShouldBehaveConsistentlyWithEstimatedCardinalitiesEvenInToughCases.

@Test
public void testCompareToShouldBehaveConsistentlyWithEstimatedCardinalitiesEvenInToughCases() throws Exception {
    // given
    Random rand = new Random(0);
    HyperUniquesAggregatorFactory factory = new HyperUniquesAggregatorFactory("foo", "bar");
    Comparator comparator = factory.getComparator();
    for (int i = 0; i < 1000; ++i) {
        // given
        HyperLogLogCollector leftCollector = HyperLogLogCollector.makeLatestCollector();
        int j = rand.nextInt(9000) + 5000;
        for (int l = 0; l < j; ++l) {
            leftCollector.add(fn.hashLong(rand.nextLong()).asBytes());
        }
        HyperLogLogCollector rightCollector = HyperLogLogCollector.makeLatestCollector();
        int k = rand.nextInt(9000) + 5000;
        for (int l = 0; l < k; ++l) {
            rightCollector.add(fn.hashLong(rand.nextLong()).asBytes());
        }
        // when
        final int orderedByCardinality = Double.compare(leftCollector.estimateCardinality(), rightCollector.estimateCardinality());
        final int orderedByComparator = comparator.compare(leftCollector, rightCollector);
        // then, assert hyperloglog comparator behaves consistently with estimated cardinalities
        Assert.assertEquals(String.format("orderedByComparator=%d, orderedByCardinality=%d,\n" + "Left={cardinality=%f, hll=%s},\n" + "Right={cardinality=%f, hll=%s},\n", orderedByComparator, orderedByCardinality, leftCollector.estimateCardinality(), leftCollector, rightCollector.estimateCardinality(), rightCollector), orderedByCardinality, orderedByComparator);
    }
}
Also used : Random(java.util.Random) HyperLogLogCollector(io.druid.hll.HyperLogLogCollector) Comparator(java.util.Comparator) Test(org.junit.Test)

Aggregations

Comparator (java.util.Comparator)322 ArrayList (java.util.ArrayList)123 List (java.util.List)58 Test (org.junit.Test)58 HashMap (java.util.HashMap)50 IOException (java.io.IOException)36 Map (java.util.Map)35 File (java.io.File)24 HashSet (java.util.HashSet)23 TreeSet (java.util.TreeSet)20 Set (java.util.Set)18 Iterator (java.util.Iterator)15 Method (java.lang.reflect.Method)14 Collections (java.util.Collections)14 Date (java.util.Date)14 TreeMap (java.util.TreeMap)14 ArrayMap (android.util.ArrayMap)12 Collection (java.util.Collection)11 LinkedList (java.util.LinkedList)11 SimpleDateFormat (java.text.SimpleDateFormat)10