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());
}
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);
}
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);
}
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();
}
}
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);
}
Aggregations