weixiangsun commented on a change in pull request #8029: URL: https://github.com/apache/pinot/pull/8029#discussion_r788272952
########## File path: pinot-core/src/main/java/org/apache/pinot/core/query/reduce/PreAggregationGapFillSelectionOperatorService.java ########## @@ -0,0 +1,388 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.core.query.reduce; + +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Set; +import org.apache.pinot.common.request.context.ExpressionContext; +import org.apache.pinot.common.request.context.FilterContext; +import org.apache.pinot.common.request.context.FunctionContext; +import org.apache.pinot.common.utils.DataSchema; +import org.apache.pinot.common.utils.DataSchema.ColumnDataType; +import org.apache.pinot.common.utils.DataTable; +import org.apache.pinot.core.common.BlockValSet; +import org.apache.pinot.core.data.table.Key; +import org.apache.pinot.core.query.aggregation.function.AggregationFunction; +import org.apache.pinot.core.query.aggregation.function.AggregationFunctionFactory; +import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder; +import org.apache.pinot.core.query.request.context.QueryContext; +import org.apache.pinot.core.query.selection.SelectionOperatorUtils; +import org.apache.pinot.core.util.GapfillUtils; +import org.apache.pinot.spi.data.DateTimeFormatSpec; +import org.apache.pinot.spi.data.DateTimeGranularitySpec; + + +/** + * The <code>PreAggregationGapFillSelectionOperatorService</code> class provides the services for selection queries with + * <code>ORDER BY</code>. + * <p>Expected behavior: + * <ul> + * <li> + * Return selection results with the same order of columns as user passed in. + * <ul> + * <li>Eg. SELECT colB, colA, colC FROM table -> [valB, valA, valC]</li> + * </ul> + * </li> + * <li> + * For 'SELECT *', return columns with alphabetically order. + * <ul> + * <li>Eg. SELECT * FROM table -> [valA, valB, valC]</li> + * </ul> + * </li> + * <li> + * Order by does not change the order of columns in selection results. + * <ul> + * <li>Eg. SELECT colB, colA, colC FROM table ORDER BY calC -> [valB, valA, valC]</li> + * </ul> + * </li> + * </ul> + */ +@SuppressWarnings("rawtypes") +public class PreAggregationGapFillSelectionOperatorService { + private final List<String> _columns; + private final DataSchema _dataSchema; + private final int _limitForAggregatedResult; + private final int _limitForGapfilledResult; + private final PriorityQueue<Object[]> _rows; + + private final DateTimeGranularitySpec _dateTimeGranularity; + private final DateTimeFormatSpec _dateTimeFormatter; + private final long _startMs; + private final long _endMs; + private final long _timeBucketSize; + private final QueryContext _queryContext; + private final QueryContext _preAggregateGapFillQueryContext; + + private final int _numOfGroupByKeys; + private final List<Integer> _groupByKeyIndexes; + private final boolean [] _isGroupBySelections; + private final Set<Key> _groupByKeys; + private final List<Key> _groupByKeyList; + private final Map<Key, Integer> _groupByKeyMappings; + private final Map<Key, Object[]> _previousByGroupKey; + private final Map<String, ExpressionContext> _fillExpressions; + private final FilterContext _gapFillFilterContext; + + /** + * Constructor for <code>SelectionOperatorService</code> with {@link DataSchema}. (Inter segment) + * + * @param queryContext Selection order-by query + * @param dataSchema data schema. + */ + public PreAggregationGapFillSelectionOperatorService(QueryContext queryContext, DataSchema dataSchema) { + _columns = Arrays.asList(dataSchema.getColumnNames()); + _dataSchema = dataSchema; + _limitForAggregatedResult = queryContext.getLimit(); + _limitForGapfilledResult = queryContext.getSubQueryContext().getLimit(); + _rows = new PriorityQueue<>(Math.min(_limitForAggregatedResult, + SelectionOperatorUtils.MAX_ROW_HOLDER_INITIAL_CAPACITY), + getTypeCompatibleComparator()); + + _queryContext = queryContext; + _preAggregateGapFillQueryContext = queryContext.getSubQueryContext(); + ExpressionContext gapFillSelection = + GapfillUtils.getPreAggregateGapfillExpressionContext(_preAggregateGapFillQueryContext); + + List<ExpressionContext> args = gapFillSelection.getFunction().getArguments(); + Preconditions.checkArgument( + args.size() > 5, "PreAggregateGapFill does not have correct number of arguments."); + Preconditions.checkArgument( + args.get(1).getLiteral() != null, "The second argument of PostAggregateGapFill should be TimeFormatter."); + Preconditions.checkArgument( + args.get(2).getLiteral() != null, "The third argument of PostAggregateGapFill should be start time."); + Preconditions.checkArgument( + args.get(3).getLiteral() != null, "The fourth argument of PostAggregateGapFill should be end time."); + Preconditions.checkArgument( + args.get(4).getLiteral() != null, "The fifth argument of PostAggregateGapFill should be time bucket size."); + + _gapFillFilterContext = _queryContext.getFilter(); + _dateTimeFormatter = new DateTimeFormatSpec(args.get(1).getLiteral()); + String start = args.get(2).getLiteral(); + String end = args.get(3).getLiteral(); + _dateTimeGranularity = new DateTimeGranularitySpec(args.get(4).getLiteral()); + _startMs = truncate(_dateTimeFormatter.fromFormatToMillis(start)); + _endMs = truncate(_dateTimeFormatter.fromFormatToMillis(end)); + _timeBucketSize = _dateTimeGranularity.granularityToMillis(); + + ExpressionContext timeseriesOn = GapfillUtils.getTimeSeriesOnExpressionContext(gapFillSelection); + _fillExpressions = GapfillUtils.getFillExpressions(gapFillSelection); + + _previousByGroupKey = new HashMap<>(); + _groupByKeyIndexes = new ArrayList<>(); + _isGroupBySelections = new boolean[dataSchema.getColumnDataTypes().length]; + _groupByKeys = new HashSet<>(); + _groupByKeyList = new ArrayList<>(); + _groupByKeyMappings = new HashMap<>(); + + Map<String, Integer> indexes = new HashMap<>(); + for (int i = 0; i < _columns.size(); i++) { + indexes.put(_columns.get(i), i); + } + + Preconditions.checkArgument(timeseriesOn != null, "The TimeSeriesOn expressions should be specified."); + _numOfGroupByKeys = timeseriesOn.getFunction().getArguments().size() - 1; + List<ExpressionContext> timeSeries = timeseriesOn.getFunction().getArguments(); + // The first one argument of timeSeries is time column. The left ones are defining entity. + for (int i = 1; i < timeSeries.size(); i++) { + int index = indexes.get(timeSeries.get(i).getIdentifier()); + _isGroupBySelections[index] = true; + _groupByKeyIndexes.add(index); + } + } + + private Key constructGroupKeys(Object[] row) { + Object [] groupKeys = new Object[_numOfGroupByKeys]; + for (int i = 0; i < _numOfGroupByKeys; i++) { + groupKeys[i] = row[_groupByKeyIndexes.get(i)]; + } + return new Key(groupKeys); + } + + private long truncate(long epoch) { + int sz = _dateTimeGranularity.getSize(); + return epoch / sz * sz; + } + + List<Object[]> gapFillAndAggregate(List<Object[]> sortedRows, DataSchema dataSchemaForAggregatedResult) { Review comment: break it into the small functions. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org