blob: a6f04936f5dc481acb190525e11d5c92a33c25fe [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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 org.apache.datasketches.hive.tuple;
import java.util.List;
import java.util.Random;
import org.apache.hadoop.io.BytesWritable;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.apache.datasketches.tuple.ArrayOfDoublesUpdatableSketch;
import org.apache.datasketches.tuple.ArrayOfDoublesUpdatableSketchBuilder;
@SuppressWarnings("javadoc")
public class ArrayOfDoublesSketchToVariancesUDFTest {
@Test
public void nullSketch() {
List<Double> result = new ArrayOfDoublesSketchToVariancesUDF().evaluate(null);
Assert.assertNull(result);
}
@Test
public void emptySketch() {
ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().build();
List<Double> result = new ArrayOfDoublesSketchToVariancesUDF().evaluate(new BytesWritable(sketch.compact().toByteArray()));
Assert.assertNull(result);
}
@Test
public void oneEntrySketch() {
ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().build();
sketch.update(1, new double[] {1});
List<Double> result = new ArrayOfDoublesSketchToVariancesUDF().evaluate(new BytesWritable(sketch.compact().toByteArray()));
Assert.assertNotNull(result);
Assert.assertEquals(result.size(), 1);
Assert.assertEquals(result.get(0), 0.0);
}
@Test
public void manyEntriesTwoValuesSketch() {
ArrayOfDoublesUpdatableSketch sketch = new ArrayOfDoublesUpdatableSketchBuilder().setNumberOfValues(2).build();
Random rand = new Random(0);
int numKeys = 10000; // to saturate the sketch with default number of nominal entries (4K)
for (int i = 0; i < numKeys; i++ ) {
// two random values normally distributed with standard deviations of 1 and 10
sketch.update(i, new double[] {rand.nextGaussian(), rand.nextGaussian() * 10.0});
}
Assert.assertTrue(sketch.getRetainedEntries() >= 4096);
List<Double> result = new ArrayOfDoublesSketchToVariancesUDF().evaluate(new BytesWritable(sketch.compact().toByteArray()));
Assert.assertNotNull(result);
Assert.assertEquals(result.size(), 2);
Assert.assertEquals(result.get(0), 1.0, 0.04);
Assert.assertEquals(result.get(1), 100.0, 100.0 * 0.04); // squared standard deviation within 4%
}
}