alpass163 commented on code in PR #19227:
URL: https://github.com/apache/iotdb/pull/19227#discussion_r4547054679


##########
iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/source/relational/aggregation/integral/IntegralFunctionUtils.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.calc.execution.operator.source.relational.aggregation.integral;
+
+import 
org.apache.iotdb.calc.execution.operator.source.relational.aggregation.rate.TimeValueBuffer;
+import org.apache.iotdb.calc.i18n.CalcMessages;
+import org.apache.iotdb.commons.exception.SemanticException;
+
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.enums.TSDataType;
+
+import static org.apache.iotdb.calc.utils.constant.SqlConstant.INTEGRAL;
+
+/**
+ * Helpers shared by the non-grouped and the grouped implementations of 
integral(), so that value
+ * reading, validation messages and the trapezoidal accumulation stay 
identical in both paths.
+ */
+public final class IntegralFunctionUtils {
+
+  private IntegralFunctionUtils() {}
+
+  public static double readValue(TSDataType valueDataType, Column column, int 
position) {
+    final double value =
+        switch (valueDataType) {
+          case INT32 -> column.getInt(position);
+          case INT64 -> column.getLong(position);
+          case FLOAT -> column.getFloat(position);
+          case DOUBLE -> column.getDouble(position);
+          default ->
+              throw new SemanticException(
+                  String.format(
+                      CalcMessages
+                          
.EXCEPTION_AGGREGATE_FUNCTION_ARG_DOES_NOT_SUPPORT_VALUE_TYPE_ARG_9DD7388D,
+                      INTEGRAL,
+                      valueDataType));
+        };
+    if (!Double.isFinite(value)) {
+      throw new SemanticException(
+          String.format(
+              CalcMessages
+                  
.EXCEPTION_AGGREGATE_FUNCTION_ARG_DOES_NOT_SUPPORT_NON_FINITE_VALUE_COL_ARG_AC2AAC62,
+              INTEGRAL,
+              value));
+    }
+    return value;
+  }
+
+  /** Sorts the samples by time and sums the trapezoidal area of adjacent 
distinct timestamps. */
+  public static double calculateArea(TimeValueBuffer samples) {
+    // fewer than two effective points have no time span, so the area is 0
+    if (samples == null || samples.size() < 2) {
+      return 0.0;
+    }
+    samples.sort();

Review Comment:
   1. issue one[Duplicate timestamps give non-deterministic results.]: change 
the requirements document, accept the non-deterministic results when the 
samestamps occurs;
   2. issue two[The window-function form is quadratic.] : it's acceptable 



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

Reply via email to