Search in sources :

Example 26 with FieldAccessPostAggregator

use of org.apache.druid.query.aggregation.post.FieldAccessPostAggregator in project druid by druid-io.

the class MomentSketchAggregatorFactoryTest method testResultArraySignature.

@Test
public void testResultArraySignature() {
    final TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource("dummy").intervals("2000/3000").granularity(Granularities.HOUR).aggregators(new CountAggregatorFactory("count"), new MomentSketchAggregatorFactory("moment", "col", null, null), new MomentSketchMergeAggregatorFactory("momentMerge", null, null)).postAggregators(new FieldAccessPostAggregator("moment-access", "moment"), new FinalizingFieldAccessPostAggregator("moment-finalize", "moment"), new FieldAccessPostAggregator("momentMerge-access", "momentMerge"), new FinalizingFieldAccessPostAggregator("momentMerge-finalize", "momentMerge")).build();
    Assert.assertEquals(RowSignature.builder().addTimeColumn().add("count", ColumnType.LONG).add("moment", MomentSketchAggregatorFactory.TYPE).add("momentMerge", MomentSketchAggregatorFactory.TYPE).add("moment-access", MomentSketchAggregatorFactory.TYPE).add("moment-finalize", MomentSketchAggregatorFactory.TYPE).add("momentMerge-access", MomentSketchAggregatorFactory.TYPE).add("momentMerge-finalize", MomentSketchAggregatorFactory.TYPE).build(), new TimeseriesQueryQueryToolChest().resultArraySignature(query));
}
Also used : FinalizingFieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) FinalizingFieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) Test(org.junit.Test)

Example 27 with FieldAccessPostAggregator

use of org.apache.druid.query.aggregation.post.FieldAccessPostAggregator in project druid by druid-io.

the class PostAveragerAggregatorCalculatorTest method setup.

@Before
public void setup() {
    System.setProperty("druid.generic.useDefaultValueForNull", "true");
    NullHandling.initializeForTests();
    MovingAverageQuery query = new MovingAverageQuery(new TableDataSource("d"), new MultipleIntervalSegmentSpec(Collections.singletonList(new Interval("2017-01-01/2017-01-01", ISOChronology.getInstanceUTC()))), null, Granularities.DAY, null, Collections.singletonList(new CountAggregatorFactory("count")), Collections.emptyList(), null, Collections.singletonList(new DoubleMeanAveragerFactory("avgCount", 7, 1, "count")), Collections.singletonList(new ArithmeticPostAggregator("avgCountRatio", "/", Arrays.asList(new FieldAccessPostAggregator("count", "count"), new FieldAccessPostAggregator("avgCount", "avgCount")))), null, null);
    pac = new PostAveragerAggregatorCalculator(query);
    event = new HashMap<>();
    row = new MapBasedRow(new DateTime(ISOChronology.getInstanceUTC()), event);
}
Also used : MapBasedRow(org.apache.druid.data.input.MapBasedRow) ArithmeticPostAggregator(org.apache.druid.query.aggregation.post.ArithmeticPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) TableDataSource(org.apache.druid.query.TableDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) DoubleMeanAveragerFactory(org.apache.druid.query.movingaverage.averagers.DoubleMeanAveragerFactory) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Before(org.junit.Before)

Example 28 with FieldAccessPostAggregator

use of org.apache.druid.query.aggregation.post.FieldAccessPostAggregator in project druid by druid-io.

the class TDigestSketchQuantileSqlAggregator method toDruidAggregation.

@Nullable
@Override
public Aggregation toDruidAggregation(final PlannerContext plannerContext, final RowSignature rowSignature, final VirtualColumnRegistry virtualColumnRegistry, final RexBuilder rexBuilder, final String name, final AggregateCall aggregateCall, final Project project, final List<Aggregation> existingAggregations, final boolean finalizeAggregations) {
    // This is expected to be a tdigest sketch
    final DruidExpression input = Aggregations.toDruidExpressionForNumericAggregator(plannerContext, rowSignature, Expressions.fromFieldAccess(rowSignature, project, aggregateCall.getArgList().get(0)));
    if (input == null) {
        return null;
    }
    final AggregatorFactory aggregatorFactory;
    final String sketchName = StringUtils.format("%s:agg", name);
    // this is expected to be quantile fraction
    final RexNode quantileArg = Expressions.fromFieldAccess(rowSignature, project, aggregateCall.getArgList().get(1));
    if (!quantileArg.isA(SqlKind.LITERAL)) {
        // Quantile must be a literal in order to plan.
        return null;
    }
    final double quantile = ((Number) RexLiteral.value(quantileArg)).floatValue();
    Integer compression = TDigestSketchAggregatorFactory.DEFAULT_COMPRESSION;
    if (aggregateCall.getArgList().size() > 2) {
        final RexNode compressionArg = Expressions.fromFieldAccess(rowSignature, project, aggregateCall.getArgList().get(2));
        compression = ((Number) RexLiteral.value(compressionArg)).intValue();
    }
    // Look for existing matching aggregatorFactory.
    for (final Aggregation existing : existingAggregations) {
        for (AggregatorFactory factory : existing.getAggregatorFactories()) {
            if (factory instanceof TDigestSketchAggregatorFactory) {
                final boolean matches = TDigestSketchUtils.matchingAggregatorFactoryExists(virtualColumnRegistry, input, compression, (TDigestSketchAggregatorFactory) factory);
                if (matches) {
                    // Found existing one. Use this.
                    return Aggregation.create(ImmutableList.of(), new TDigestSketchToQuantilePostAggregator(name, new FieldAccessPostAggregator(factory.getName(), factory.getName()), quantile));
                }
            }
        }
    }
    // No existing match found. Create a new one.
    if (input.isDirectColumnAccess()) {
        aggregatorFactory = new TDigestSketchAggregatorFactory(sketchName, input.getDirectColumn(), compression);
    } else {
        String virtualColumnName = virtualColumnRegistry.getOrCreateVirtualColumnForExpression(input, ColumnType.FLOAT);
        aggregatorFactory = new TDigestSketchAggregatorFactory(sketchName, virtualColumnName, compression);
    }
    return Aggregation.create(ImmutableList.of(aggregatorFactory), new TDigestSketchToQuantilePostAggregator(name, new FieldAccessPostAggregator(sketchName, sketchName), quantile));
}
Also used : Aggregation(org.apache.druid.sql.calcite.aggregation.Aggregation) TDigestSketchAggregatorFactory(org.apache.druid.query.aggregation.tdigestsketch.TDigestSketchAggregatorFactory) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) TDigestSketchToQuantilePostAggregator(org.apache.druid.query.aggregation.tdigestsketch.TDigestSketchToQuantilePostAggregator) TDigestSketchAggregatorFactory(org.apache.druid.query.aggregation.tdigestsketch.TDigestSketchAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Example 29 with FieldAccessPostAggregator

use of org.apache.druid.query.aggregation.post.FieldAccessPostAggregator in project druid by druid-io.

the class TDigestSketchSqlAggregatorTest method testQuantileOnCastedString.

@Test
public void testQuantileOnCastedString() throws Exception {
    cannotVectorize();
    testQuery("SELECT\n" + "  TDIGEST_QUANTILE(CAST(dim1 AS DOUBLE), 0.0),\n" + "  TDIGEST_QUANTILE(CAST(dim1 AS DOUBLE), 0.5),\n" + "  TDIGEST_QUANTILE(CAST(dim1 AS DOUBLE), 1.0)\n" + "FROM foo", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).virtualColumns(new ExpressionVirtualColumn("v0", "CAST(\"dim1\", 'DOUBLE')", ColumnType.FLOAT, ExprMacroTable.nil())).aggregators(new TDigestSketchAggregatorFactory("a0:agg", "v0", 100)).postAggregators(new TDigestSketchToQuantilePostAggregator("a0", new FieldAccessPostAggregator("a0:agg", "a0:agg"), 0.0), new TDigestSketchToQuantilePostAggregator("a1", new FieldAccessPostAggregator("a0:agg", "a0:agg"), 0.5), new TDigestSketchToQuantilePostAggregator("a2", new FieldAccessPostAggregator("a0:agg", "a0:agg"), 1.0)).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(NullHandling.replaceWithDefault() ? new Object[] { 0.0, 0.5, 10.1 } : new Object[] { 1.0, 2.0, 10.1 }));
}
Also used : TDigestSketchAggregatorFactory(org.apache.druid.query.aggregation.tdigestsketch.TDigestSketchAggregatorFactory) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) TDigestSketchToQuantilePostAggregator(org.apache.druid.query.aggregation.tdigestsketch.TDigestSketchToQuantilePostAggregator) BaseCalciteQueryTest(org.apache.druid.sql.calcite.BaseCalciteQueryTest) Test(org.junit.Test)

Example 30 with FieldAccessPostAggregator

use of org.apache.druid.query.aggregation.post.FieldAccessPostAggregator in project druid by druid-io.

the class TDigestSketchAggregatorFactoryTest method testResultArraySignature.

@Test
public void testResultArraySignature() {
    final TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource("dummy").intervals("2000/3000").granularity(Granularities.HOUR).aggregators(new CountAggregatorFactory("count"), new TDigestSketchAggregatorFactory("tdigest", "col", null)).postAggregators(new FieldAccessPostAggregator("tdigest-access", "tdigest"), new FinalizingFieldAccessPostAggregator("tdigest-finalize", "tdigest")).build();
    Assert.assertEquals(RowSignature.builder().addTimeColumn().add("count", ColumnType.LONG).add("tdigest", TDigestSketchAggregatorFactory.TYPE).add("tdigest-access", TDigestSketchAggregatorFactory.TYPE).add("tdigest-finalize", TDigestSketchAggregatorFactory.TYPE).build(), new TimeseriesQueryQueryToolChest().resultArraySignature(query));
}
Also used : FinalizingFieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) FinalizingFieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) Test(org.junit.Test)

Aggregations

FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)108 Test (org.junit.Test)97 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)45 ArithmeticPostAggregator (org.apache.druid.query.aggregation.post.ArithmeticPostAggregator)32 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)30 FinalizingFieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator)24 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)22 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)22 TimeseriesQuery (org.apache.druid.query.timeseries.TimeseriesQuery)17 TimeseriesQueryQueryToolChest (org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest)17 ConstantPostAggregator (org.apache.druid.query.aggregation.post.ConstantPostAggregator)15 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)15 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)14 DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)12 HashMap (java.util.HashMap)11 DoubleSumAggregatorFactory (org.apache.druid.query.aggregation.DoubleSumAggregatorFactory)11 QueryDataSource (org.apache.druid.query.QueryDataSource)10 Aggregator (org.apache.druid.query.aggregation.Aggregator)10 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)10 BaseCalciteQueryTest (org.apache.druid.sql.calcite.BaseCalciteQueryTest)10