CRZbulabula commented on a change in pull request #4375: URL: https://github.com/apache/iotdb/pull/4375#discussion_r749913393
########## File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillWithValueFilterDataSet.java ########## @@ -0,0 +1,438 @@ +/* + * 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.iotdb.db.query.dataset.groupby; + +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.db.engine.StorageEngine; +import org.apache.iotdb.db.engine.storagegroup.StorageGroupProcessor; +import org.apache.iotdb.db.exception.StorageEngineException; +import org.apache.iotdb.db.exception.metadata.IllegalPathException; +import org.apache.iotdb.db.exception.query.QueryProcessException; +import org.apache.iotdb.db.metadata.PartialPath; +import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan; +import org.apache.iotdb.db.qp.physical.crud.RawDataQueryPlan; +import org.apache.iotdb.db.query.aggregation.AggregateResult; +import org.apache.iotdb.db.query.context.QueryContext; +import org.apache.iotdb.db.query.control.QueryResourceManager; +import org.apache.iotdb.db.query.executor.fill.IFill; +import org.apache.iotdb.db.query.executor.fill.LinearFill; +import org.apache.iotdb.db.query.executor.fill.PreviousFill; +import org.apache.iotdb.db.query.factory.AggregateResultFactory; +import org.apache.iotdb.db.query.reader.series.IReaderByTimestamp; +import org.apache.iotdb.db.query.reader.series.SeriesReaderByTimestamp; +import org.apache.iotdb.db.query.timegenerator.ServerTimeGenerator; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.read.expression.IExpression; +import org.apache.iotdb.tsfile.read.expression.impl.BinaryExpression; +import org.apache.iotdb.tsfile.read.expression.impl.SingleSeriesExpression; +import org.apache.iotdb.tsfile.read.filter.GroupByFilter; +import org.apache.iotdb.tsfile.read.filter.basic.BinaryFilter; +import org.apache.iotdb.tsfile.read.filter.basic.Filter; +import org.apache.iotdb.tsfile.read.query.timegenerator.TimeGenerator; +import org.apache.iotdb.tsfile.utils.Pair; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class GroupByFillWithValueFilterDataSet extends GroupByFillEngineDataSet { + + // the next query time range of each path + private List<TimeGenerator> timestampGenerators; + private List<TimeGenerator> extraPreviousGenerators; + private List<TimeGenerator> extraNextGenerators; + + // data reader lists + private List<IReaderByTimestamp> allDataReaderList; + private List<IReaderByTimestamp> extraPreviousDataReaderList; + private List<IReaderByTimestamp> extraNextDataReaderList; + + // cached timestamp for next group by partition + private long lastTimestamp; + private List<LinkedList<Long>> cachedTimestamps; + + private final int timeStampFetchSize = IoTDBDescriptor.getInstance().getConfig().getBatchSize(); + + /** constructor. */ + public GroupByFillWithValueFilterDataSet( + QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan) + throws StorageEngineException, QueryProcessException { + super(context, groupByTimeFillPlan); + + initPathGenerators(context, groupByTimeFillPlan); + + initExtraGenerators(context, groupByTimeFillPlan); + if (extraPreviousGenerators != null) { + initExtraArrays(extraPreviousValues, extraPreviousTimes, true, extraPreviousGenerators); + } + if (extraNextGenerators != null) { + initExtraArrays(extraNextValues, extraNextTimes, false, extraNextGenerators); + } + + initCachedTimesAndValues(); + } + + private void initPathGenerators(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan) Review comment: done ########## File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillWithValueFilterDataSet.java ########## @@ -0,0 +1,438 @@ +/* + * 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.iotdb.db.query.dataset.groupby; + +import org.apache.iotdb.db.conf.IoTDBDescriptor; +import org.apache.iotdb.db.engine.StorageEngine; +import org.apache.iotdb.db.engine.storagegroup.StorageGroupProcessor; +import org.apache.iotdb.db.exception.StorageEngineException; +import org.apache.iotdb.db.exception.metadata.IllegalPathException; +import org.apache.iotdb.db.exception.query.QueryProcessException; +import org.apache.iotdb.db.metadata.PartialPath; +import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan; +import org.apache.iotdb.db.qp.physical.crud.RawDataQueryPlan; +import org.apache.iotdb.db.query.aggregation.AggregateResult; +import org.apache.iotdb.db.query.context.QueryContext; +import org.apache.iotdb.db.query.control.QueryResourceManager; +import org.apache.iotdb.db.query.executor.fill.IFill; +import org.apache.iotdb.db.query.executor.fill.LinearFill; +import org.apache.iotdb.db.query.executor.fill.PreviousFill; +import org.apache.iotdb.db.query.factory.AggregateResultFactory; +import org.apache.iotdb.db.query.reader.series.IReaderByTimestamp; +import org.apache.iotdb.db.query.reader.series.SeriesReaderByTimestamp; +import org.apache.iotdb.db.query.timegenerator.ServerTimeGenerator; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.read.expression.IExpression; +import org.apache.iotdb.tsfile.read.expression.impl.BinaryExpression; +import org.apache.iotdb.tsfile.read.expression.impl.SingleSeriesExpression; +import org.apache.iotdb.tsfile.read.filter.GroupByFilter; +import org.apache.iotdb.tsfile.read.filter.basic.BinaryFilter; +import org.apache.iotdb.tsfile.read.filter.basic.Filter; +import org.apache.iotdb.tsfile.read.query.timegenerator.TimeGenerator; +import org.apache.iotdb.tsfile.utils.Pair; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class GroupByFillWithValueFilterDataSet extends GroupByFillEngineDataSet { + + // the next query time range of each path + private List<TimeGenerator> timestampGenerators; + private List<TimeGenerator> extraPreviousGenerators; + private List<TimeGenerator> extraNextGenerators; + + // data reader lists + private List<IReaderByTimestamp> allDataReaderList; + private List<IReaderByTimestamp> extraPreviousDataReaderList; + private List<IReaderByTimestamp> extraNextDataReaderList; + + // cached timestamp for next group by partition + private long lastTimestamp; + private List<LinkedList<Long>> cachedTimestamps; + + private final int timeStampFetchSize = IoTDBDescriptor.getInstance().getConfig().getBatchSize(); + + /** constructor. */ + public GroupByFillWithValueFilterDataSet( + QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan) + throws StorageEngineException, QueryProcessException { + super(context, groupByTimeFillPlan); + + initPathGenerators(context, groupByTimeFillPlan); + + initExtraGenerators(context, groupByTimeFillPlan); + if (extraPreviousGenerators != null) { + initExtraArrays(extraPreviousValues, extraPreviousTimes, true, extraPreviousGenerators); + } + if (extraNextGenerators != null) { + initExtraArrays(extraNextValues, extraNextTimes, false, extraNextGenerators); + } + + initCachedTimesAndValues(); + } + + private void initPathGenerators(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan) + throws QueryProcessException, StorageEngineException { + this.timestampGenerators = new ArrayList<>(); + this.cachedTimestamps = new ArrayList<>(); + for (int i = 0; i < deduplicatedPaths.size(); i++) { + timestampGenerators.add(getTimeGenerator(context, groupByTimeFillPlan)); + cachedTimestamps.add(new LinkedList<>()); + } + + this.allDataReaderList = new ArrayList<>(); + List<StorageGroupProcessor> list = + StorageEngine.getInstance() + .mergeLock(paths.stream().map(p -> (PartialPath) p).collect(Collectors.toList())); + try { + for (int i = 0; i < paths.size(); i++) { + PartialPath path = (PartialPath) paths.get(i); + allDataReaderList.add( + getReaderByTime(path, groupByTimeFillPlan, dataTypes.get(i), context)); + } + } finally { + StorageEngine.getInstance().mergeUnLock(list); + } + } + + private TimeGenerator getTimeGenerator(QueryContext context, RawDataQueryPlan queryPlan) + throws StorageEngineException { + return new ServerTimeGenerator(context, queryPlan); + } + + private IReaderByTimestamp getReaderByTime( + PartialPath path, RawDataQueryPlan queryPlan, TSDataType dataType, QueryContext context) + throws StorageEngineException, QueryProcessException { + return new SeriesReaderByTimestamp( + path, + queryPlan.getAllMeasurementsInDevice(path.getDevice()), + dataType, + context, + QueryResourceManager.getInstance().getQueryDataSource(path, context, null), + null, + ascending); + } + + private void replaceGroupByFilter(IExpression expression, Filter timeFilter) + throws QueryProcessException, IllegalPathException { + if (expression instanceof SingleSeriesExpression) { + ((SingleSeriesExpression) expression) + .setSeriesPath( + new PartialPath(((SingleSeriesExpression) expression).getSeriesPath().getFullPath())); + if (((SingleSeriesExpression) expression).getFilter() instanceof GroupByFilter) { + ((SingleSeriesExpression) expression).setFilter(timeFilter); + } else if (((SingleSeriesExpression) expression).getFilter() instanceof BinaryFilter) { + if (((BinaryFilter) ((SingleSeriesExpression) expression).getFilter()).getLeft() + instanceof GroupByFilter) { + ((BinaryFilter) ((SingleSeriesExpression) expression).getFilter()).setLeft(timeFilter); + } else if (((BinaryFilter) ((SingleSeriesExpression) expression).getFilter()).getRight() + instanceof GroupByFilter) { + ((BinaryFilter) ((SingleSeriesExpression) expression).getFilter()).setRight(timeFilter); + } + } else { + throw new QueryProcessException("unknown filter type, can't replace group by filter"); + } + } else if (expression instanceof BinaryExpression) { + replaceGroupByFilter(((BinaryExpression) expression).getLeft(), timeFilter); + replaceGroupByFilter(((BinaryExpression) expression).getRight(), timeFilter); + } else { + throw new QueryProcessException("unknown expression type, can't replace group by filter"); + } + } + + // get new expression that can query extra range + private IExpression getNewExpression(GroupByTimeFillPlan groupByTimeFillPlan, Filter timeFilter) + throws QueryProcessException { + IExpression newExpression = groupByTimeFillPlan.getExpression().clone(); + try { + replaceGroupByFilter(newExpression, timeFilter); + } catch (IllegalPathException ignore) { + // ignored + } + return newExpression; + } + + /* Init extra path executors to query data outside the original group by query */ + private void initExtraGenerators(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan) Review comment: done -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
