Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,10 @@ private static Integer getRowsPerSegment(CompactionTask compactionTask)
private static RowSignature getRowSignature(DataSchema dataSchema)
{
RowSignature.Builder rowSignatureBuilder = RowSignature.builder();
rowSignatureBuilder.add(dataSchema.getTimestampSpec().getTimestampColumn(), ColumnType.LONG);
if (dataSchema.getDimensionsSpec().isForceSegmentSortByTime() == true) {
// If sort not forced by time, __time appears as part of dimensions in DimensionsSpec
rowSignatureBuilder.add(dataSchema.getTimestampSpec().getTimestampColumn(), ColumnType.LONG);
}
if (!isQueryGranularityEmptyOrNone(dataSchema)) {
// A virtual column for query granularity would have been added. Add corresponding column type.
rowSignatureBuilder.add(TIME_VIRTUAL_COLUMN, ColumnType.LONG);
Expand Down Expand Up @@ -359,25 +362,31 @@ private static List<DimensionSpec> getAggregateDimensions(

private static ColumnMappings getColumnMappings(DataSchema dataSchema)
{
List<ColumnMapping> columnMappings = dataSchema.getDimensionsSpec()
.getDimensions()
.stream()
.map(dim -> new ColumnMapping(
dim.getName(), dim.getName()))
.collect(Collectors.toList());
List<ColumnMapping> columnMappings = new ArrayList<>();
// For scan queries, a virtual column is created from __time if a custom query granularity is provided. For
// group-by queries, as insert needs __time, it will always be one of the dimensions. Since dimensions in groupby
// aren't allowed to have time column as the output name, we map time dimension to TIME_VIRTUAL_COLUMN in
// dimensions, and map it back to the time column here.
String timeColumn = (isGroupBy(dataSchema) || !isQueryGranularityEmptyOrNone(dataSchema))
? TIME_VIRTUAL_COLUMN
: ColumnHolder.TIME_COLUMN_NAME;
ColumnMapping timeColumnMapping = new ColumnMapping(timeColumn, ColumnHolder.TIME_COLUMN_NAME);
if (dataSchema.getDimensionsSpec().isForceSegmentSortByTime()) {
// When not sorted by time, the __time column is missing from dimensionsSpec
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// When not sorted by time, the __time column is missing from dimensionsSpec
// When sorted by time, the __time column is missing from dimensionsSpec

columnMappings.add(timeColumnMapping);
}
columnMappings.addAll(
dataSchema.getDimensionsSpec()
.getDimensions()
.stream()
.map(dim -> dim.getName().equals(ColumnHolder.TIME_COLUMN_NAME)
? timeColumnMapping
: new ColumnMapping(dim.getName(), dim.getName()))
.collect(Collectors.toList())
);
columnMappings.addAll(Arrays.stream(dataSchema.getAggregators())
.map(agg -> new ColumnMapping(agg.getName(), agg.getName()))
.collect(
Collectors.toList()));
if (isGroupBy(dataSchema) || !isQueryGranularityEmptyOrNone(dataSchema)) {
// For scan queries, a virtual column is created from __time if a custom query granularity is provided. For
// group-by queries, as insert needs __time, it will always be one of the dimensions. Since dimensions in groupby
// aren't allowed to have time column as the output name, we map time dimension to TIME_VIRTUAL_COLUMN in
// dimensions, and map it back to the time column here.
columnMappings.add(new ColumnMapping(TIME_VIRTUAL_COLUMN, ColumnHolder.TIME_COLUMN_NAME));
} else {
columnMappings.add(new ColumnMapping(ColumnHolder.TIME_COLUMN_NAME, ColumnHolder.TIME_COLUMN_NAME));
}
.collect(Collectors.toList()));
return new ColumnMappings(columnMappings);
}

Expand All @@ -392,6 +401,19 @@ private static List<OrderByColumnSpec> getOrderBySpec(PartitionsSpec partitionSp
return Collections.emptyList();
}

private static Map<String, Object> buildQueryContext(
Map<String, Object> taskContext,
DataSchema dataSchema
)
{
if (dataSchema.getDimensionsSpec().isForceSegmentSortByTime()) {
return taskContext;
}
Map<String, Object> queryContext = new HashMap<>(taskContext);
queryContext.put(MultiStageQueryContext.CTX_FORCE_TIME_SORT, false);
return queryContext;
}

private static Query<?> buildScanQuery(
CompactionTask compactionTask,
Interval interval,
Expand All @@ -408,7 +430,7 @@ private static Query<?> buildScanQuery(
.columnTypes(rowSignature.getColumnTypes())
.intervals(new MultipleIntervalSegmentSpec(Collections.singletonList(interval)))
.filters(dataSchema.getTransformSpec().getFilter())
.context(compactionTask.getContext());
.context(buildQueryContext(compactionTask.getContext(), dataSchema));

if (compactionTask.getTuningConfig() != null && compactionTask.getTuningConfig().getPartitionsSpec() != null) {
List<OrderByColumnSpec> orderByColumnSpecs = getOrderBySpec(compactionTask.getTuningConfig().getPartitionsSpec());
Expand Down Expand Up @@ -560,7 +582,7 @@ private Query<?> buildGroupByQuery(
.setDimensions(getAggregateDimensions(dataSchema, inputColToVirtualCol))
.setAggregatorSpecs(Arrays.asList(dataSchema.getAggregators()))
.setPostAggregatorSpecs(postAggregators)
.setContext(compactionTask.getContext())
.setContext(buildQueryContext(compactionTask.getContext(), dataSchema))
.setInterval(interval);

if (compactionTask.getTuningConfig() != null && compactionTask.getTuningConfig().getPartitionsSpec() != null) {
Expand Down
Loading