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