Search in sources :

Example 6 with ColumnType

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

the class DruidSchema method analysisToRowSignature.

@VisibleForTesting
static RowSignature analysisToRowSignature(final SegmentAnalysis analysis) {
    final RowSignature.Builder rowSignatureBuilder = RowSignature.builder();
    for (Map.Entry<String, ColumnAnalysis> entry : analysis.getColumns().entrySet()) {
        if (entry.getValue().isError()) {
            // Skip columns with analysis errors.
            continue;
        }
        ColumnType valueType = entry.getValue().getTypeSignature();
        // flavor of COMPLEX.
        if (valueType == null) {
            // likelyhood of upgrading from some version lower than 0.23 is low
            try {
                valueType = ColumnType.fromString(entry.getValue().getType());
            } catch (IllegalArgumentException ignored) {
                valueType = ColumnType.UNKNOWN_COMPLEX;
            }
        }
        rowSignatureBuilder.add(entry.getKey(), valueType);
    }
    return ROW_SIGNATURE_INTERNER.intern(rowSignatureBuilder.build());
}
Also used : ColumnType(org.apache.druid.segment.column.ColumnType) ColumnAnalysis(org.apache.druid.query.metadata.metadata.ColumnAnalysis) RowSignature(org.apache.druid.segment.column.RowSignature) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) TreeMap(java.util.TreeMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with ColumnType

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

the class VirtualColumnRegistry method getVirtualColumnByExpression.

/**
 * @deprecated use {@link #getVirtualColumnByExpression(DruidExpression, RelDataType)} instead
 */
@Deprecated
@Nullable
public VirtualColumn getVirtualColumnByExpression(String expression, RelDataType type) {
    final ColumnType columnType = Calcites.getColumnTypeForRelDataType(type);
    ExpressionAndTypeHint wrapped = wrap(DruidExpression.fromExpression(expression), columnType);
    return Optional.ofNullable(virtualColumnsByExpression.get(wrapped)).map(this::getVirtualColumn).orElse(null);
}
Also used : ColumnType(org.apache.druid.segment.column.ColumnType) Nullable(javax.annotation.Nullable)

Example 8 with ColumnType

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

the class Projection method postAggregatorDirectColumnIsOk.

/**
 * Returns true if a post-aggregation "expression" can be realized as a direct field access. This is true if it's
 * a direct column access that doesn't require an implicit cast.
 *
 * @param aggregateRowSignature signature of the aggregation
 * @param expression            post-aggregation expression
 * @param rexNode               RexNode for the post-aggregation expression
 *
 * @return yes or no
 */
private static boolean postAggregatorDirectColumnIsOk(final RowSignature aggregateRowSignature, final DruidExpression expression, final RexNode rexNode) {
    if (!expression.isDirectColumnAccess()) {
        return false;
    }
    // We don't really have a way to cast complex type. So might as well not do anything and return.
    final ColumnType columnValueType = aggregateRowSignature.getColumnType(expression.getDirectColumn()).orElseThrow(() -> new ISE("Encountered null type for column[%s]", expression.getDirectColumn()));
    if (columnValueType.is(ValueType.COMPLEX)) {
        return true;
    }
    // Check if a cast is necessary.
    final ExpressionType toExprType = ExpressionType.fromColumnTypeStrict(columnValueType);
    final ExpressionType fromExprType = ExpressionType.fromColumnTypeStrict(Calcites.getColumnTypeForRelDataType(rexNode.getType()));
    return toExprType.equals(fromExprType);
}
Also used : ColumnType(org.apache.druid.segment.column.ColumnType) ISE(org.apache.druid.java.util.common.ISE) ExpressionType(org.apache.druid.math.expr.ExpressionType)

Example 9 with ColumnType

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

the class ScanQueryQueryToolChest method resultArraySignature.

@Override
public RowSignature resultArraySignature(final ScanQuery query) {
    if (query.getColumns() == null || query.getColumns().isEmpty()) {
        // will include none of them.
        return RowSignature.empty();
    } else {
        final RowSignature.Builder builder = RowSignature.builder();
        if (query.withNonNullLegacy(scanQueryConfig).isLegacy()) {
            builder.add(ScanQueryEngine.LEGACY_TIMESTAMP_KEY, null);
        }
        for (String columnName : query.getColumns()) {
            // With the Scan query we only know the columnType for virtual columns. Let's report those, at least.
            final ColumnType columnType;
            final VirtualColumn virtualColumn = query.getVirtualColumns().getVirtualColumn(columnName);
            if (virtualColumn != null) {
                columnType = virtualColumn.capabilities(columnName).toColumnType();
            } else {
                // Unknown type. In the future, it would be nice to have a way to fill these in.
                columnType = null;
            }
            builder.add(columnName, columnType);
        }
        return builder.build();
    }
}
Also used : ColumnType(org.apache.druid.segment.column.ColumnType) VirtualColumn(org.apache.druid.segment.VirtualColumn) RowSignature(org.apache.druid.segment.column.RowSignature)

Example 10 with ColumnType

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

the class BuiltinApproxCountDistinctSqlAggregator 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) {
    // Don't use Aggregations.getArgumentsForSimpleAggregator, since it won't let us use direct column access
    // for string columns.
    final RexNode rexNode = Expressions.fromFieldAccess(rowSignature, project, Iterables.getOnlyElement(aggregateCall.getArgList()));
    final DruidExpression arg = Expressions.toDruidExpression(plannerContext, rowSignature, rexNode);
    if (arg == null) {
        return null;
    }
    final AggregatorFactory aggregatorFactory;
    final String aggregatorName = finalizeAggregations ? Calcites.makePrefixedName(name, "a") : name;
    if (arg.isDirectColumnAccess() && rowSignature.getColumnType(arg.getDirectColumn()).map(type -> type.is(ValueType.COMPLEX)).orElse(false)) {
        aggregatorFactory = new HyperUniquesAggregatorFactory(aggregatorName, arg.getDirectColumn(), false, true);
    } else {
        final RelDataType dataType = rexNode.getType();
        final ColumnType inputType = Calcites.getColumnTypeForRelDataType(dataType);
        if (inputType == null) {
            throw new ISE("Cannot translate sqlTypeName[%s] to Druid type for field[%s]", dataType.getSqlTypeName(), aggregatorName);
        }
        final DimensionSpec dimensionSpec;
        if (arg.isSimpleExtraction()) {
            dimensionSpec = arg.getSimpleExtraction().toDimensionSpec(null, inputType);
        } else {
            String virtualColumnName = virtualColumnRegistry.getOrCreateVirtualColumnForExpression(arg, dataType);
            dimensionSpec = new DefaultDimensionSpec(virtualColumnName, null, inputType);
        }
        aggregatorFactory = new CardinalityAggregatorFactory(aggregatorName, null, ImmutableList.of(dimensionSpec), false, true);
    }
    return Aggregation.create(Collections.singletonList(aggregatorFactory), finalizeAggregations ? new HyperUniqueFinalizingPostAggregator(name, aggregatorFactory.getName()) : null);
}
Also used : DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ColumnType(org.apache.druid.segment.column.ColumnType) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) HyperUniquesAggregatorFactory(org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) ISE(org.apache.druid.java.util.common.ISE) HyperUniqueFinalizingPostAggregator(org.apache.druid.query.aggregation.hyperloglog.HyperUniqueFinalizingPostAggregator) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) HyperUniquesAggregatorFactory(org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Aggregations

ColumnType (org.apache.druid.segment.column.ColumnType)43 Nullable (javax.annotation.Nullable)16 ISE (org.apache.druid.java.util.common.ISE)15 RowSignature (org.apache.druid.segment.column.RowSignature)14 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)13 RexNode (org.apache.calcite.rex.RexNode)12 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)12 List (java.util.List)11 IAE (org.apache.druid.java.util.common.IAE)11 RelDataType (org.apache.calcite.rel.type.RelDataType)9 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)9 Collectors (java.util.stream.Collectors)8 ArrayList (java.util.ArrayList)7 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)5 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)5 StringComparator (org.apache.druid.query.ordering.StringComparator)5 Aggregation (org.apache.druid.sql.calcite.aggregation.Aggregation)5 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)4 Preconditions (com.google.common.base.Preconditions)4 Collections (java.util.Collections)4