weixiangsun commented on a change in pull request #8029: URL: https://github.com/apache/pinot/pull/8029#discussion_r830751576
########## File path: pinot-core/src/main/java/org/apache/pinot/core/query/reduce/GapfillProcessor.java ########## @@ -0,0 +1,471 @@ +/** + * 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.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.pinot.common.request.context.ExpressionContext; +import org.apache.pinot.common.request.context.FunctionContext; +import org.apache.pinot.common.response.broker.BrokerResponseNative; +import org.apache.pinot.common.response.broker.ResultTable; +import org.apache.pinot.common.utils.DataSchema; +import org.apache.pinot.common.utils.DataSchema.ColumnDataType; +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.function.CountAggregationFunction; +import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder; +import org.apache.pinot.core.query.request.context.QueryContext; +import org.apache.pinot.core.util.GapfillUtils; +import org.apache.pinot.spi.data.DateTimeFormatSpec; +import org.apache.pinot.spi.data.DateTimeGranularitySpec; + + +/** + * Helper class to reduce and set gap fill results into the BrokerResponseNative + */ +@SuppressWarnings({"rawtypes", "unchecked"}) +public class GapfillProcessor { + private final QueryContext _queryContext; + + private final int _limitForAggregatedResult; + private final DateTimeGranularitySpec _gapfillDateTimeGranularity; + private final DateTimeGranularitySpec _postGapfillDateTimeGranularity; + private final DateTimeFormatSpec _dateTimeFormatter; + private final long _startMs; + private final long _endMs; + private final long _gapfillTimeBucketSize; + private final long _postGapfillTimeBucketSize; + private final int _numOfTimeBuckets; + private final List<Integer> _groupByKeyIndexes; + private final Set<Key> _groupByKeys; + private final Map<Key, Object[]> _previousByGroupKey; + private final Map<String, ExpressionContext> _fillExpressions; + private final List<ExpressionContext> _timeSeries; + private final GapfillUtils.GapfillType _gapfillType; + private int _limitForGapfilledResult; + private boolean[] _isGroupBySelections; + private final int _timeBucketColumnIndex; + private int[] _sourceColumnIndexForResultSchema = null; + private final int _aggregationSize; + + GapfillProcessor(QueryContext queryContext, GapfillUtils.GapfillType gapfillType) { + _queryContext = queryContext; + _gapfillType = gapfillType; + _limitForAggregatedResult = queryContext.getLimit(); + if (_gapfillType == GapfillUtils.GapfillType.AGGREGATE_GAP_FILL + || _gapfillType == GapfillUtils.GapfillType.GAP_FILL) { + _limitForGapfilledResult = queryContext.getLimit(); + } else { + _limitForGapfilledResult = queryContext.getSubquery().getLimit(); + } + + ExpressionContext gapFillSelection = GapfillUtils.getGapfillExpressionContext(queryContext, _gapfillType); + _timeBucketColumnIndex = GapfillUtils.findTimeBucketColumnIndex(queryContext, _gapfillType); + + List<ExpressionContext> args = gapFillSelection.getFunction().getArguments(); + + _dateTimeFormatter = new DateTimeFormatSpec(args.get(1).getLiteral()); + _gapfillDateTimeGranularity = new DateTimeGranularitySpec(args.get(4).getLiteral()); + _postGapfillDateTimeGranularity = new DateTimeGranularitySpec(args.get(5).getLiteral()); Review comment: Here is background information. Currently we only have one time bucket size. Usually one entity only has one representative state for one time bucket. Take the parking lot as an example, one parking lot can be occupied or not. If we want to know how long the park lots are occupied per day inside the parking building. The time bucket size is one day. The parking lot state will be decided by one event within the time bucket. By introducing the different granularity for gapfill and post-gapfill, it will make it possible to calculate the above metrics more precisely. For parking lot, we can introduce 5 minutes as gapfill bucket size, then aggregate all occupied states within post-gapfill time bucket (1 day). Please let me know if you have any question about it. @Jackie-Jiang -- 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