Search in sources :

Example 1 with SqlAggregator

use of org.apache.druid.sql.calcite.aggregation.SqlAggregator in project druid by druid-io.

the class DruidOperatorTable method lookupOperatorOverloads.

@Override
public void lookupOperatorOverloads(final SqlIdentifier opName, final SqlFunctionCategory category, final SqlSyntax syntax, final List<SqlOperator> operatorList, final SqlNameMatcher nameMatcher) {
    if (opName == null) {
        return;
    }
    if (opName.names.size() != 1) {
        return;
    }
    final OperatorKey operatorKey = OperatorKey.of(opName.getSimple(), syntax);
    final SqlAggregator aggregator = aggregators.get(operatorKey);
    if (aggregator != null) {
        operatorList.add(aggregator.calciteFunction());
    }
    final SqlOperatorConversion operatorConversion = operatorConversions.get(operatorKey);
    if (operatorConversion != null) {
        operatorList.add(operatorConversion.calciteOperator());
    }
    final SqlOperator convertletOperator = CONVERTLET_OPERATORS.get(operatorKey);
    if (convertletOperator != null) {
        operatorList.add(convertletOperator);
    }
}
Also used : EarliestLatestBySqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.EarliestLatestBySqlAggregator) ArrayConcatSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.ArrayConcatSqlAggregator) MaxSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.MaxSqlAggregator) MinSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.MinSqlAggregator) BuiltinApproxCountDistinctSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.BuiltinApproxCountDistinctSqlAggregator) SqlAggregator(org.apache.druid.sql.calcite.aggregation.SqlAggregator) StringSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.StringSqlAggregator) BitwiseSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.BitwiseSqlAggregator) ArraySqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.ArraySqlAggregator) SumZeroSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.SumZeroSqlAggregator) AvgSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.AvgSqlAggregator) GroupingSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.GroupingSqlAggregator) EarliestLatestAnySqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.EarliestLatestAnySqlAggregator) SumSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.SumSqlAggregator) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlOperatorConversion(org.apache.druid.sql.calcite.expression.SqlOperatorConversion)

Example 2 with SqlAggregator

use of org.apache.druid.sql.calcite.aggregation.SqlAggregator in project druid by druid-io.

the class DruidOperatorTable method getOperatorList.

@Override
public List<SqlOperator> getOperatorList() {
    final List<SqlOperator> retVal = new ArrayList<>();
    for (SqlAggregator aggregator : aggregators.values()) {
        retVal.add(aggregator.calciteFunction());
    }
    for (SqlOperatorConversion operatorConversion : operatorConversions.values()) {
        retVal.add(operatorConversion.calciteOperator());
    }
    retVal.addAll(DruidConvertletTable.knownOperators());
    return retVal;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) EarliestLatestBySqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.EarliestLatestBySqlAggregator) ArrayConcatSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.ArrayConcatSqlAggregator) MaxSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.MaxSqlAggregator) MinSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.MinSqlAggregator) BuiltinApproxCountDistinctSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.BuiltinApproxCountDistinctSqlAggregator) SqlAggregator(org.apache.druid.sql.calcite.aggregation.SqlAggregator) StringSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.StringSqlAggregator) BitwiseSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.BitwiseSqlAggregator) ArraySqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.ArraySqlAggregator) SumZeroSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.SumZeroSqlAggregator) AvgSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.AvgSqlAggregator) GroupingSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.GroupingSqlAggregator) EarliestLatestAnySqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.EarliestLatestAnySqlAggregator) SumSqlAggregator(org.apache.druid.sql.calcite.aggregation.builtin.SumSqlAggregator) ArrayList(java.util.ArrayList) SqlOperatorConversion(org.apache.druid.sql.calcite.expression.SqlOperatorConversion)

Example 3 with SqlAggregator

use of org.apache.druid.sql.calcite.aggregation.SqlAggregator in project druid by druid-io.

the class GroupByRules method translateAggregateCall.

/**
 * Translate an AggregateCall to Druid equivalents.
 *
 * @return translated aggregation, or null if translation failed.
 */
public static Aggregation translateAggregateCall(final PlannerContext plannerContext, final RowSignature rowSignature, final VirtualColumnRegistry virtualColumnRegistry, final RexBuilder rexBuilder, final Project project, final List<Aggregation> existingAggregations, final String name, final AggregateCall call, final boolean finalizeAggregations) {
    final DimFilter filter;
    if (call.filterArg >= 0) {
        // AGG(xxx) FILTER(WHERE yyy)
        if (project == null) {
            // We need some kind of projection to support filtered aggregations.
            return null;
        }
        final RexNode expression = project.getChildExps().get(call.filterArg);
        final DimFilter nonOptimizedFilter = Expressions.toFilter(plannerContext, rowSignature, virtualColumnRegistry, expression);
        if (nonOptimizedFilter == null) {
            return null;
        } else {
            filter = Filtration.create(nonOptimizedFilter).optimizeFilterOnly(virtualColumnRegistry.getFullRowSignature()).getDimFilter();
        }
    } else {
        filter = null;
    }
    final SqlAggregator sqlAggregator = plannerContext.getOperatorTable().lookupAggregator(call.getAggregation());
    if (sqlAggregator == null) {
        return null;
    }
    // Compute existingAggregations for SqlAggregator impls that want it.
    final List<Aggregation> existingAggregationsWithSameFilter = new ArrayList<>();
    for (Aggregation existingAggregation : existingAggregations) {
        if (filter == null) {
            final boolean doesMatch = existingAggregation.getAggregatorFactories().stream().noneMatch(factory -> factory instanceof FilteredAggregatorFactory);
            if (doesMatch) {
                existingAggregationsWithSameFilter.add(existingAggregation);
            }
        } else {
            final boolean doesMatch = existingAggregation.getAggregatorFactories().stream().allMatch(factory -> factory instanceof FilteredAggregatorFactory && ((FilteredAggregatorFactory) factory).getFilter().equals(filter));
            if (doesMatch) {
                existingAggregationsWithSameFilter.add(Aggregation.create(existingAggregation.getAggregatorFactories().stream().map(factory -> ((FilteredAggregatorFactory) factory).getAggregator()).collect(Collectors.toList()), existingAggregation.getPostAggregator()));
            }
        }
    }
    final Aggregation retVal = sqlAggregator.toDruidAggregation(plannerContext, rowSignature, virtualColumnRegistry, rexBuilder, name, call, project, existingAggregationsWithSameFilter, finalizeAggregations);
    if (retVal == null) {
        return null;
    } else {
        // Check if this refers to the existingAggregationsWithSameFilter. If so, no need to apply the filter.
        if (isUsingExistingAggregation(retVal, existingAggregationsWithSameFilter)) {
            return retVal;
        } else {
            return retVal.filter(rowSignature, virtualColumnRegistry, filter);
        }
    }
}
Also used : Aggregation(org.apache.druid.sql.calcite.aggregation.Aggregation) FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) Project(org.apache.calcite.rel.core.Project) RexBuilder(org.apache.calcite.rex.RexBuilder) SqlAggregator(org.apache.druid.sql.calcite.aggregation.SqlAggregator) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) Set(java.util.Set) Aggregation(org.apache.druid.sql.calcite.aggregation.Aggregation) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) DimFilter(org.apache.druid.query.filter.DimFilter) RexNode(org.apache.calcite.rex.RexNode) RowSignature(org.apache.druid.segment.column.RowSignature) VirtualColumnRegistry(org.apache.druid.sql.calcite.rel.VirtualColumnRegistry) PlannerContext(org.apache.druid.sql.calcite.planner.PlannerContext) AggregateCall(org.apache.calcite.rel.core.AggregateCall) Expressions(org.apache.druid.sql.calcite.expression.Expressions) Filtration(org.apache.druid.sql.calcite.filtration.Filtration) SqlAggregator(org.apache.druid.sql.calcite.aggregation.SqlAggregator) ArrayList(java.util.ArrayList) DimFilter(org.apache.druid.query.filter.DimFilter) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

SqlAggregator (org.apache.druid.sql.calcite.aggregation.SqlAggregator)3 ArrayList (java.util.ArrayList)2 SqlOperator (org.apache.calcite.sql.SqlOperator)2 ArrayConcatSqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.ArrayConcatSqlAggregator)2 ArraySqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.ArraySqlAggregator)2 AvgSqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.AvgSqlAggregator)2 BitwiseSqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.BitwiseSqlAggregator)2 BuiltinApproxCountDistinctSqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.BuiltinApproxCountDistinctSqlAggregator)2 EarliestLatestAnySqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.EarliestLatestAnySqlAggregator)2 EarliestLatestBySqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.EarliestLatestBySqlAggregator)2 GroupingSqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.GroupingSqlAggregator)2 MaxSqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.MaxSqlAggregator)2 MinSqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.MinSqlAggregator)2 StringSqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.StringSqlAggregator)2 SumSqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.SumSqlAggregator)2 SumZeroSqlAggregator (org.apache.druid.sql.calcite.aggregation.builtin.SumZeroSqlAggregator)2 SqlOperatorConversion (org.apache.druid.sql.calcite.expression.SqlOperatorConversion)2 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1