You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/05/30 02:22:43 UTC

[GitHub] [iotdb] Alima777 commented on a change in pull request #3289: [IOTDB-1391] Add a new Aggregation function ext

Alima777 commented on a change in pull request #3289:
URL: https://github.com/apache/iotdb/pull/3289#discussion_r642007406



##########
File path: server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/ExtremeAggrResult.java
##########
@@ -0,0 +1,210 @@
+/*
+ * 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.aggregation.impl;
+
+import org.apache.iotdb.db.query.aggregation.AggregateResult;
+import org.apache.iotdb.db.query.aggregation.AggregationType;
+import org.apache.iotdb.db.query.reader.series.IReaderByTimestamp;
+import org.apache.iotdb.tsfile.exception.write.UnSupportedDataTypeException;
+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.BatchData;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+public class ExtremeAggrResult extends AggregateResult {
+
+  public ExtremeAggrResult(TSDataType dataType) {
+    super(dataType, AggregationType.EXTREME);
+    reset();
+  }
+
+  public Object getAbsValue(Object v) {
+    double doubleValue;
+    float floatValue;
+    int intValue;
+    long longValue;
+
+    switch (resultDataType) {
+      case DOUBLE:
+        doubleValue = (Double) v;
+        return Math.abs(doubleValue);
+      case FLOAT:
+        floatValue = (Float) v;
+        return Math.abs(floatValue);
+      case INT32:
+        intValue = (Integer) v;
+        return Math.abs(intValue);
+      case INT64:
+        longValue = (Long) v;
+        return Math.abs(longValue);
+      default:
+        throw new UnSupportedDataTypeException(String.valueOf(resultDataType));
+    }
+  }
+
+  public Comparable<Object> compareAbsVal(Comparable<Object> minVal, Comparable<Object> maxVal) {
+    Comparable<Object> extVal;
+
+    Comparable<Object> absMaxVal = (Comparable<Object>) getAbsValue(maxVal);
+    Comparable<Object> absMinVal = (Comparable<Object>) getAbsValue(minVal);
+
+    if (absMaxVal.compareTo(absMinVal) >= 0) {
+      extVal = maxVal;
+    } else {
+      extVal = minVal;
+    }
+    return extVal;
+  }
+
+  @Override
+  public Object getResult() {
+    return hasCandidateResult() ? getValue() : null;
+  }
+
+  @Override
+  public void updateResultFromStatistics(Statistics statistics) {
+    Comparable<Object> maxVal = (Comparable<Object>) statistics.getMaxValue();
+    Comparable<Object> minVal = (Comparable<Object>) statistics.getMinValue();
+
+    Comparable<Object> extVal = compareAbsVal(minVal, maxVal);
+    updateResult(extVal);
+  }
+
+  @Override
+  public void updateResultFromPageData(BatchData dataInThisPage) {
+    updateResultFromPageData(dataInThisPage, Long.MIN_VALUE, Long.MAX_VALUE);
+  }
+
+  @Override
+  public void updateResultFromPageData(BatchData dataInThisPage, long minBound, long maxBound) {
+    Comparable<Object> extVal = null;
+
+    while (dataInThisPage.hasCurrent()
+        && dataInThisPage.currentTime() < maxBound
+        && dataInThisPage.currentTime() >= minBound) {
+      Comparable<Object> currentValue = (Comparable<Object>) dataInThisPage.currentValue();
+      Comparable<Object> absCurrentValue = (Comparable<Object>) getAbsValue(currentValue);
+
+      if (extVal == null) {
+        extVal = currentValue;
+      } else {
+        Comparable<Object> absExtVal = (Comparable<Object>) getAbsValue(extVal);
+        if (absExtVal.compareTo(absCurrentValue) < 0) {
+          extVal = currentValue;
+        } else if (absExtVal.compareTo(absCurrentValue) == 0) {
+          if (extVal.compareTo(currentValue) > 0) {
+            extVal = currentValue;
+          }
+        }
+      }
+
+      dataInThisPage.next();
+    }
+    updateResult(extVal);
+  }

Review comment:
       Please see `MaxValueAggrResult.java` to rewrite this method. Your code logic is confusing.

##########
File path: server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/ExtremeAggrResult.java
##########
@@ -0,0 +1,210 @@
+/*
+ * 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.aggregation.impl;
+
+import org.apache.iotdb.db.query.aggregation.AggregateResult;
+import org.apache.iotdb.db.query.aggregation.AggregationType;
+import org.apache.iotdb.db.query.reader.series.IReaderByTimestamp;
+import org.apache.iotdb.tsfile.exception.write.UnSupportedDataTypeException;
+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.BatchData;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+public class ExtremeAggrResult extends AggregateResult {
+
+  public ExtremeAggrResult(TSDataType dataType) {
+    super(dataType, AggregationType.EXTREME);
+    reset();
+  }
+
+  public Object getAbsValue(Object v) {
+    double doubleValue;
+    float floatValue;
+    int intValue;
+    long longValue;
+
+    switch (resultDataType) {
+      case DOUBLE:
+        doubleValue = (Double) v;
+        return Math.abs(doubleValue);

Review comment:
       There is no need to use global varaible.
   ```suggestion
   
       switch (resultDataType) {
         case DOUBLE:
           return Math.abs((Double) v);
   ```

##########
File path: server/src/main/java/org/apache/iotdb/db/query/aggregation/impl/ExtremeAggrResult.java
##########
@@ -0,0 +1,210 @@
+/*
+ * 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.aggregation.impl;
+
+import org.apache.iotdb.db.query.aggregation.AggregateResult;
+import org.apache.iotdb.db.query.aggregation.AggregationType;
+import org.apache.iotdb.db.query.reader.series.IReaderByTimestamp;
+import org.apache.iotdb.tsfile.exception.write.UnSupportedDataTypeException;
+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.BatchData;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+public class ExtremeAggrResult extends AggregateResult {
+
+  public ExtremeAggrResult(TSDataType dataType) {
+    super(dataType, AggregationType.EXTREME);
+    reset();
+  }
+
+  public Object getAbsValue(Object v) {
+    double doubleValue;
+    float floatValue;
+    int intValue;
+    long longValue;
+
+    switch (resultDataType) {
+      case DOUBLE:
+        doubleValue = (Double) v;
+        return Math.abs(doubleValue);
+      case FLOAT:
+        floatValue = (Float) v;
+        return Math.abs(floatValue);
+      case INT32:
+        intValue = (Integer) v;
+        return Math.abs(intValue);
+      case INT64:
+        longValue = (Long) v;
+        return Math.abs(longValue);
+      default:
+        throw new UnSupportedDataTypeException(String.valueOf(resultDataType));
+    }
+  }
+
+  public Comparable<Object> compareAbsVal(Comparable<Object> minVal, Comparable<Object> maxVal) {
+    Comparable<Object> extVal;
+
+    Comparable<Object> absMaxVal = (Comparable<Object>) getAbsValue(maxVal);
+    Comparable<Object> absMinVal = (Comparable<Object>) getAbsValue(minVal);
+
+    if (absMaxVal.compareTo(absMinVal) >= 0) {
+      extVal = maxVal;
+    } else {
+      extVal = minVal;
+    }
+    return extVal;
+  }

Review comment:
       ```suggestion
     public Comparable<Object> compareAbsVal(Comparable<Object> minVal, Comparable<Object> maxVal) {
     Comparable<Object> absMaxVal = (Comparable<Object>) getAbsValue(maxVal);
       Comparable<Object> absMinVal = (Comparable<Object>) getAbsValue(minVal);
       return absMaxVal.compare(absMinVal) >= 0 ? maxVal : minval;
   ```




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org