Search in sources :

Example 6 with VarianceAggregatorFactory

use of org.apache.druid.query.aggregation.variance.VarianceAggregatorFactory in project druid by druid-io.

the class VarianceSqlAggregatorTest method testVarianceOrderBy.

@Test
public void testVarianceOrderBy() throws Exception {
    List<Object[]> expectedResults = NullHandling.sqlCompatible() ? ImmutableList.of(new Object[] { "a", 0f }, new Object[] { null, 0f }, new Object[] { "", 0f }, new Object[] { "abc", null }) : ImmutableList.of(new Object[] { "a", 0.5f }, new Object[] { "", 0.0033333334f }, new Object[] { "abc", 0f });
    testQuery("select dim2, VARIANCE(f1) from druid.numfoo group by 1 order by 2 desc", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE3).setInterval(new MultipleIntervalSegmentSpec(ImmutableList.of(Filtration.eternity()))).setGranularity(Granularities.ALL).setDimensions(new DefaultDimensionSpec("dim2", "_d0")).setAggregatorSpecs(new VarianceAggregatorFactory("a0:agg", "f1", "sample", "float")).setLimitSpec(DefaultLimitSpec.builder().orderBy(new OrderByColumnSpec("a0:agg", OrderByColumnSpec.Direction.DESCENDING, StringComparators.NUMERIC)).build()).setContext(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT).build()), expectedResults);
}
Also used : OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) VarianceAggregatorFactory(org.apache.druid.query.aggregation.variance.VarianceAggregatorFactory) BaseCalciteQueryTest(org.apache.druid.sql.calcite.BaseCalciteQueryTest) Test(org.junit.Test)

Example 7 with VarianceAggregatorFactory

use of org.apache.druid.query.aggregation.variance.VarianceAggregatorFactory in project druid by druid-io.

the class VarianceSqlAggregatorTest method testStdDevSamp.

@Test
public void testStdDevSamp() throws Exception {
    VarianceAggregatorCollector holder1 = new VarianceAggregatorCollector();
    VarianceAggregatorCollector holder2 = new VarianceAggregatorCollector();
    VarianceAggregatorCollector holder3 = new VarianceAggregatorCollector();
    for (InputRow row : CalciteTests.ROWS1_WITH_NUMERIC_DIMS) {
        Object raw1 = row.getRaw("d1");
        Object raw2 = row.getRaw("f1");
        Object raw3 = row.getRaw("l1");
        addToHolder(holder1, raw1);
        addToHolder(holder2, raw2);
        addToHolder(holder3, raw3);
    }
    final List<Object[]> expectedResults = ImmutableList.of(new Object[] { Math.sqrt(holder1.getVariance(false)), (float) Math.sqrt(holder2.getVariance(false)), (long) Math.sqrt(holder3.getVariance(false)) });
    testQuery("SELECT\n" + "STDDEV_SAMP(d1),\n" + "STDDEV_SAMP(f1),\n" + "STDDEV_SAMP(l1)\n" + "FROM numfoo", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE3).intervals(new MultipleIntervalSegmentSpec(ImmutableList.of(Filtration.eternity()))).granularity(Granularities.ALL).aggregators(ImmutableList.of(new VarianceAggregatorFactory("a0:agg", "d1", "sample", "double"), new VarianceAggregatorFactory("a1:agg", "f1", "sample", "float"), new VarianceAggregatorFactory("a2:agg", "l1", "sample", "long"))).postAggregators(new StandardDeviationPostAggregator("a0", "a0:agg", "sample"), new StandardDeviationPostAggregator("a1", "a1:agg", "sample"), new StandardDeviationPostAggregator("a2", "a2:agg", "sample")).context(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT).build()), expectedResults);
}
Also used : VarianceAggregatorCollector(org.apache.druid.query.aggregation.variance.VarianceAggregatorCollector) StandardDeviationPostAggregator(org.apache.druid.query.aggregation.variance.StandardDeviationPostAggregator) InputRow(org.apache.druid.data.input.InputRow) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) VarianceAggregatorFactory(org.apache.druid.query.aggregation.variance.VarianceAggregatorFactory) BaseCalciteQueryTest(org.apache.druid.sql.calcite.BaseCalciteQueryTest) Test(org.junit.Test)

Aggregations

VarianceAggregatorFactory (org.apache.druid.query.aggregation.variance.VarianceAggregatorFactory)7 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)6 BaseCalciteQueryTest (org.apache.druid.sql.calcite.BaseCalciteQueryTest)6 Test (org.junit.Test)6 InputRow (org.apache.druid.data.input.InputRow)5 VarianceAggregatorCollector (org.apache.druid.query.aggregation.variance.VarianceAggregatorCollector)5 StandardDeviationPostAggregator (org.apache.druid.query.aggregation.variance.StandardDeviationPostAggregator)4 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)2 Nullable (javax.annotation.Nullable)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)1 IAE (org.apache.druid.java.util.common.IAE)1 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)1 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)1 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)1 OrderByColumnSpec (org.apache.druid.query.groupby.orderby.OrderByColumnSpec)1 ColumnType (org.apache.druid.segment.column.ColumnType)1 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)1