Wei-hao-Li commented on code in PR #9192: URL: https://github.com/apache/iotdb/pull/9192#discussion_r1122572960
########## server/src/main/java/org/apache/iotdb/db/mpp/aggregation/TimeDurationAccumulator.java: ########## @@ -0,0 +1,112 @@ +/* + * 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.mpp.aggregation; + +import org.apache.iotdb.db.mpp.execution.operator.window.IWindow; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics; +import org.apache.iotdb.tsfile.read.common.block.column.Column; +import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder; + +public class TimeDurationAccumulator implements Accumulator { + protected long minTime = Long.MAX_VALUE; + protected long maxTime = Long.MIN_VALUE; + + @Override + public int addInput(Column[] column, IWindow window, boolean ignoringNull) { + int curPositionCount = column[0].getPositionCount(); + for (int i = 0; i < curPositionCount; i++) { + // skip null value in control column + if (ignoringNull && column[0].isNull(i)) { + continue; + } + if (!window.satisfy(column[0], i)) { + return i; + } + window.mergeOnePoint(column, i); + if (!column[2].isNull(i)) { + updateMaxTime(column[1].getLong(i)); + updateMinTime(column[1].getLong(i)); + } + } + return curPositionCount; + } + + @Override + public void addIntermediate(Column[] partialResult) { + if (partialResult[0].isNull(0)) { + return; + } + updateMaxTime(partialResult[0].getLong(0)); + updateMinTime(partialResult[1].getLong(0)); + } + + @Override + public void addStatistics(Statistics statistics) { + updateMaxTime(statistics.getEndTime()); + updateMinTime(statistics.getStartTime()); + } + + @Override + public void setFinal(Column finalResult) { + if (finalResult.isNull(0)) { + return; + } + maxTime = finalResult.getLong(0); + minTime = 0L; + } + + @Override + public void outputIntermediate(ColumnBuilder[] tsBlockBuilder) { + tsBlockBuilder[0].writeLong(maxTime); + tsBlockBuilder[1].writeLong(minTime); + } + + @Override + public void outputFinal(ColumnBuilder tsBlockBuilder) { + tsBlockBuilder.writeLong(maxTime - minTime); + } + + @Override + public void reset() {} + + @Override + public boolean hasFinalResult() { + return false; + } + + @Override + public TSDataType[] getIntermediateType() { + return new TSDataType[] {TSDataType.INT64, TSDataType.INT64}; + } + + @Override + public TSDataType getFinalType() { + return TSDataType.getTsDataType((byte) 2); Review Comment: return `TSDataType.INT64` directly ########## server/src/main/java/org/apache/iotdb/db/mpp/aggregation/TimeDurationAccumulator.java: ########## @@ -0,0 +1,112 @@ +/* + * 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.mpp.aggregation; + +import org.apache.iotdb.db.mpp.execution.operator.window.IWindow; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics; +import org.apache.iotdb.tsfile.read.common.block.column.Column; +import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder; + +public class TimeDurationAccumulator implements Accumulator { + protected long minTime = Long.MAX_VALUE; + protected long maxTime = Long.MIN_VALUE; + + @Override + public int addInput(Column[] column, IWindow window, boolean ignoringNull) { + int curPositionCount = column[0].getPositionCount(); + for (int i = 0; i < curPositionCount; i++) { + // skip null value in control column + if (ignoringNull && column[0].isNull(i)) { + continue; + } + if (!window.satisfy(column[0], i)) { + return i; + } + window.mergeOnePoint(column, i); + if (!column[2].isNull(i)) { + updateMaxTime(column[1].getLong(i)); + updateMinTime(column[1].getLong(i)); + } + } + return curPositionCount; + } + + @Override + public void addIntermediate(Column[] partialResult) { + if (partialResult[0].isNull(0)) { + return; + } + updateMaxTime(partialResult[0].getLong(0)); + updateMinTime(partialResult[1].getLong(0)); + } + + @Override + public void addStatistics(Statistics statistics) { + updateMaxTime(statistics.getEndTime()); + updateMinTime(statistics.getStartTime()); + } + + @Override + public void setFinal(Column finalResult) { + if (finalResult.isNull(0)) { + return; + } + maxTime = finalResult.getLong(0); + minTime = 0L; + } + + @Override + public void outputIntermediate(ColumnBuilder[] tsBlockBuilder) { + tsBlockBuilder[0].writeLong(maxTime); + tsBlockBuilder[1].writeLong(minTime); + } + + @Override + public void outputFinal(ColumnBuilder tsBlockBuilder) { + tsBlockBuilder.writeLong(maxTime - minTime); + } Review Comment: case1: We need to output `null` if there were no data in the input; case2: If the Agg step is `STATIC`, that is input is `FINAL` & output is `FINAL`, we need to cache the input duration in method `setFinal` to output here. Fix these cases and add IT, you can add IT of case2 in 'IoTDBAggregationOptimizeIT#testStaticAggregator' . ########## integration-test/src/test/java/org/apache/iotdb/db/it/aggregation/IoTDBTimeDurationIT.java: ########## @@ -0,0 +1,381 @@ +/* + * 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.it.aggregation; + +import org.apache.iotdb.it.env.EnvFactory; +import org.apache.iotdb.it.framework.IoTDBTestRunner; +import org.apache.iotdb.itbase.category.ClusterIT; +import org.apache.iotdb.itbase.category.LocalStandaloneIT; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import static org.apache.iotdb.db.it.utils.TestUtils.assertTestFail; +import static org.apache.iotdb.db.it.utils.TestUtils.prepareData; +import static org.apache.iotdb.db.it.utils.TestUtils.resultSetEqualTest; + +@RunWith(IoTDBTestRunner.class) +@Category({LocalStandaloneIT.class, ClusterIT.class}) +public class IoTDBTimeDurationIT { Review Comment: Add GroupByTime & SlidingWindow IT ########## server/src/main/java/org/apache/iotdb/db/mpp/aggregation/TimeDurationAccumulator.java: ########## @@ -0,0 +1,112 @@ +/* + * 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.mpp.aggregation; + +import org.apache.iotdb.db.mpp.execution.operator.window.IWindow; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.file.metadata.statistics.Statistics; +import org.apache.iotdb.tsfile.read.common.block.column.Column; +import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder; + +public class TimeDurationAccumulator implements Accumulator { + protected long minTime = Long.MAX_VALUE; + protected long maxTime = Long.MIN_VALUE; + + @Override + public int addInput(Column[] column, IWindow window, boolean ignoringNull) { + int curPositionCount = column[0].getPositionCount(); + for (int i = 0; i < curPositionCount; i++) { + // skip null value in control column + if (ignoringNull && column[0].isNull(i)) { + continue; + } + if (!window.satisfy(column[0], i)) { + return i; + } + window.mergeOnePoint(column, i); + if (!column[2].isNull(i)) { + updateMaxTime(column[1].getLong(i)); + updateMinTime(column[1].getLong(i)); + } + } + return curPositionCount; + } + + @Override + public void addIntermediate(Column[] partialResult) { + if (partialResult[0].isNull(0)) { + return; + } + updateMaxTime(partialResult[0].getLong(0)); + updateMinTime(partialResult[1].getLong(0)); + } + + @Override + public void addStatistics(Statistics statistics) { + updateMaxTime(statistics.getEndTime()); + updateMinTime(statistics.getStartTime()); + } + + @Override + public void setFinal(Column finalResult) { + if (finalResult.isNull(0)) { + return; + } + maxTime = finalResult.getLong(0); + minTime = 0L; Review Comment: need to modify -- 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]
