Search in sources :

Example 41 with RowSignature

use of org.apache.druid.segment.column.RowSignature in project druid by druid-io.

the class VirtualColumnRegistry method getFullRowSignature.

/**
 * Get a signature representing the base signature plus all registered virtual columns.
 */
public RowSignature getFullRowSignature() {
    final RowSignature.Builder builder = RowSignature.builder().addAll(baseRowSignature);
    final RowSignature baseSignature = builder.build();
    for (Map.Entry<String, ExpressionAndTypeHint> virtualColumn : virtualColumnsByName.entrySet()) {
        final String columnName = virtualColumn.getKey();
        // this is expensive, maybe someday it could use the typeHint, or the inferred type, but for now use native
        // expression type inference
        builder.add(columnName, virtualColumn.getValue().getExpression().toVirtualColumn(columnName, virtualColumn.getValue().getTypeHint(), macroTable).capabilities(baseSignature, columnName).toColumnType());
    }
    return builder.build();
}
Also used : RowSignature(org.apache.druid.segment.column.RowSignature) HashMap(java.util.HashMap) Map(java.util.Map)

Example 42 with RowSignature

use of org.apache.druid.segment.column.RowSignature in project druid by druid-io.

the class DruidLogicalValuesRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    final LogicalValues values = call.rel(0);
    final List<ImmutableList<RexLiteral>> tuples = values.getTuples();
    final List<Object[]> objectTuples = tuples.stream().map(tuple -> tuple.stream().map(v -> getValueFromLiteral(v, plannerContext)).collect(Collectors.toList()).toArray(new Object[0])).collect(Collectors.toList());
    final RowSignature rowSignature = RowSignatures.fromRelDataType(values.getRowType().getFieldNames(), values.getRowType());
    final DruidTable druidTable = new DruidTable(InlineDataSource.fromIterable(objectTuples, rowSignature), rowSignature, null, true, false);
    call.transformTo(DruidQueryRel.scanValues(values, druidTable, plannerContext));
}
Also used : InlineDataSource(org.apache.druid.query.InlineDataSource) RexLiteral(org.apache.calcite.rex.RexLiteral) RowSignatures(org.apache.druid.sql.calcite.table.RowSignatures) Collectors(java.util.stream.Collectors) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) UnsupportedSQLQueryException(org.apache.druid.sql.calcite.planner.UnsupportedSQLQueryException) RelOptRule(org.apache.calcite.plan.RelOptRule) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) DruidTable(org.apache.druid.sql.calcite.table.DruidTable) RowSignature(org.apache.druid.segment.column.RowSignature) DruidQueryRel(org.apache.druid.sql.calcite.rel.DruidQueryRel) LogicalValues(org.apache.calcite.rel.logical.LogicalValues) PlannerContext(org.apache.druid.sql.calcite.planner.PlannerContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Calcites(org.apache.druid.sql.calcite.planner.Calcites) Nullable(javax.annotation.Nullable) ImmutableList(com.google.common.collect.ImmutableList) DruidTable(org.apache.druid.sql.calcite.table.DruidTable) RowSignature(org.apache.druid.segment.column.RowSignature) LogicalValues(org.apache.calcite.rel.logical.LogicalValues)

Example 43 with RowSignature

use of org.apache.druid.segment.column.RowSignature in project druid by druid-io.

the class CsvWriter method writeHeader.

@Override
public void writeHeader(final RelDataType rowType, final boolean includeTypes, final boolean includeSqlTypes) {
    final RowSignature signature = RowSignatures.fromRelDataType(rowType.getFieldNames(), rowType);
    writer.writeNext(signature.getColumnNames().toArray(new String[0]), false);
    if (includeTypes) {
        final String[] types = new String[rowType.getFieldCount()];
        for (int i = 0; i < signature.size(); i++) {
            types[i] = signature.getColumnType(i).map(TypeSignature::asTypeString).orElse(null);
        }
        writer.writeNext(types, false);
    }
    if (includeSqlTypes) {
        final String[] sqlTypes = new String[rowType.getFieldCount()];
        for (int i = 0; i < signature.size(); i++) {
            sqlTypes[i] = rowType.getFieldList().get(i).getType().getSqlTypeName().getName();
        }
        writer.writeNext(sqlTypes, false);
    }
}
Also used : TypeSignature(org.apache.druid.segment.column.TypeSignature) RowSignature(org.apache.druid.segment.column.RowSignature)

Example 44 with RowSignature

use of org.apache.druid.segment.column.RowSignature in project druid by druid-io.

the class ArrayWriter method writeHeader.

static void writeHeader(final JsonGenerator jsonGenerator, final RelDataType rowType, final boolean includeTypes, final boolean includeSqlTypes) throws IOException {
    final RowSignature signature = RowSignatures.fromRelDataType(rowType.getFieldNames(), rowType);
    jsonGenerator.writeStartArray();
    for (String columnName : signature.getColumnNames()) {
        jsonGenerator.writeString(columnName);
    }
    jsonGenerator.writeEndArray();
    if (includeTypes) {
        jsonGenerator.writeStartArray();
        for (int i = 0; i < signature.size(); i++) {
            jsonGenerator.writeString(signature.getColumnType(i).map(TypeSignature::asTypeString).orElse(null));
        }
        jsonGenerator.writeEndArray();
    }
    if (includeSqlTypes) {
        jsonGenerator.writeStartArray();
        for (int i = 0; i < signature.size(); i++) {
            jsonGenerator.writeString(rowType.getFieldList().get(i).getType().getSqlTypeName().getName());
        }
        jsonGenerator.writeEndArray();
    }
}
Also used : TypeSignature(org.apache.druid.segment.column.TypeSignature) RowSignature(org.apache.druid.segment.column.RowSignature)

Example 45 with RowSignature

use of org.apache.druid.segment.column.RowSignature 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

RowSignature (org.apache.druid.segment.column.RowSignature)46 ColumnType (org.apache.druid.segment.column.ColumnType)17 List (java.util.List)14 Test (org.junit.Test)13 Collectors (java.util.stream.Collectors)12 Nullable (javax.annotation.Nullable)11 PlannerContext (org.apache.druid.sql.calcite.planner.PlannerContext)11 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)10 Expressions (org.apache.druid.sql.calcite.expression.Expressions)10 Project (org.apache.calcite.rel.core.Project)9 RexLiteral (org.apache.calcite.rex.RexLiteral)9 RexNode (org.apache.calcite.rex.RexNode)9 SqlKind (org.apache.calcite.sql.SqlKind)9 ISE (org.apache.druid.java.util.common.ISE)9 Aggregation (org.apache.druid.sql.calcite.aggregation.Aggregation)9 ArrayList (java.util.ArrayList)8 StringUtils (org.apache.druid.java.util.common.StringUtils)8 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)8 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableSet (com.google.common.collect.ImmutableSet)6