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 2022/03/02 03:30:42 UTC

[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #5136: [IOTDB-2597] Add four functions to find suitable intervals

SteveYurongSu commented on a change in pull request #5136:
URL: https://github.com/apache/iotdb/pull/5136#discussion_r817316545



##########
File path: server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFContinuouslySatisfy.java
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.udf.builtin;
+
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.query.udf.api.UDTF;
+import org.apache.iotdb.db.query.udf.api.access.Row;
+import org.apache.iotdb.db.query.udf.api.collector.PointCollector;
+import org.apache.iotdb.db.query.udf.api.customizer.config.UDTFConfigurations;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameterValidator;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameters;
+import org.apache.iotdb.db.query.udf.api.customizer.strategy.RowByRowAccessStrategy;
+import org.apache.iotdb.db.query.udf.api.exception.UDFException;
+import org.apache.iotdb.db.query.udf.api.exception.UDFInputSeriesDataTypeNotValidException;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.utils.Pair;
+
+import java.io.IOException;
+import java.util.ArrayList;
+
+public abstract class UDTFContinuouslySatisfy implements UDTF {
+  protected Long min;
+  protected Long max;
+  protected Long defaultMax = Long.MAX_VALUE;
+  protected Long defaultMin = 0L;

Review comment:
       These 2 fields can be static final. And... Does it seem that we can merge min(max) and defaultMin(defaultMax) into 1 field?

##########
File path: server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFContinuouslySatisfy.java
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.udf.builtin;
+
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.query.udf.api.UDTF;
+import org.apache.iotdb.db.query.udf.api.access.Row;
+import org.apache.iotdb.db.query.udf.api.collector.PointCollector;
+import org.apache.iotdb.db.query.udf.api.customizer.config.UDTFConfigurations;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameterValidator;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameters;
+import org.apache.iotdb.db.query.udf.api.customizer.strategy.RowByRowAccessStrategy;
+import org.apache.iotdb.db.query.udf.api.exception.UDFException;
+import org.apache.iotdb.db.query.udf.api.exception.UDFInputSeriesDataTypeNotValidException;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.utils.Pair;
+
+import java.io.IOException;
+import java.util.ArrayList;
+
+public abstract class UDTFContinuouslySatisfy implements UDTF {
+  protected Long min;
+  protected Long max;
+  protected Long defaultMax = Long.MAX_VALUE;
+  protected Long defaultMin = 0L;
+  protected TSDataType dataType;
+  protected Long satisfyValueCount;
+  protected Long satisfyValueLastTime;
+  protected Long satisfyValueStartTime;
+
+  @Override
+  public void validate(UDFParameterValidator validator) throws UDFException {
+    setDefaultValue();
+    validator
+        .validateInputSeriesNumber(1)
+        .validateInputSeriesDataType(
+            0,
+            TSDataType.INT32,
+            TSDataType.INT64,
+            TSDataType.FLOAT,
+            TSDataType.DOUBLE,
+            TSDataType.BOOLEAN)
+        .validate(
+            args -> (Long) args[1] >= (Long) args[0],
+            "max can not be smaller than min.",
+            validator.getParameters().getLongOrDefault("min", defaultMin),
+            validator.getParameters().getLongOrDefault("max", defaultMax))
+        .validate(
+            min -> (Long) min >= defaultMin,
+            "min can not be smaller than " + defaultMin + ".",
+            validator.getParameters().getLongOrDefault("min", defaultMin));
+  }
+
+  protected abstract void setDefaultValue();
+
+  protected void setDefaultMax(Long defaultMax) {
+    this.defaultMax = defaultMax;
+  }
+
+  protected void setDefaultMin(Long defaultMin) {
+    this.defaultMin = defaultMin;
+  }
+
+  @Override
+  public void beforeStart(UDFParameters parameters, UDTFConfigurations configurations)
+      throws MetadataException, UDFInputSeriesDataTypeNotValidException {
+    satisfyValueCount = 0L;
+    satisfyValueStartTime = 0L;
+    satisfyValueLastTime = -1L;
+    intervals = new ArrayList<>(0);
+
+    dataType = parameters.getDataType(0);
+    min = parameters.getLongOrDefault("min", defaultMin);
+    max = parameters.getLongOrDefault("max", defaultMax);
+    configurations
+        .setAccessStrategy(new RowByRowAccessStrategy())
+        .setOutputDataType(TSDataType.INT64);
+  }
+
+  @Override
+  public void transform(Row row, PointCollector collector)
+      throws IOException, UDFInputSeriesDataTypeNotValidException {
+    switch (dataType) {
+      case INT32:
+        transformInt(row.getTime(), row.getInt(0));
+        break;
+      case INT64:
+        transformLong(row.getTime(), row.getLong(0));
+        break;
+      case FLOAT:
+        transformFloat(row.getTime(), row.getFloat(0));
+        break;
+      case DOUBLE:
+        transformDouble(row.getTime(), row.getDouble(0));
+        break;
+      case BOOLEAN:
+        transformBoolean(row.getTime(), row.getBoolean(0));
+        break;
+      default:
+        // This will not happen
+        throw new UDFInputSeriesDataTypeNotValidException(
+            0, dataType, TSDataType.INT32, TSDataType.INT64, TSDataType.FLOAT, TSDataType.DOUBLE);
+    }
+  }
+
+  protected void transformDouble(long time, double value) {
+    if (satisfyDouble(value) && satisfyValueCount == 0L) {
+      satisfyValueCount++;
+      satisfyValueStartTime = time;
+      satisfyValueLastTime = time;
+    } else if (satisfyDouble(value)) {
+      satisfyValueCount++;
+      satisfyValueLastTime = time;
+    } else {
+      if (getRecord() >= min && getRecord() <= max && satisfyValueCount > 0) {
+        intervals.add(new Pair<>(satisfyValueStartTime, getRecord()));

Review comment:
       Try to output the result to PointCollector here. It's not recommended to keep all results in memory, because there is a risk to cause OOM.

##########
File path: server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFContinuouslySatisfy.java
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.udf.builtin;
+
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.query.udf.api.UDTF;
+import org.apache.iotdb.db.query.udf.api.access.Row;
+import org.apache.iotdb.db.query.udf.api.collector.PointCollector;
+import org.apache.iotdb.db.query.udf.api.customizer.config.UDTFConfigurations;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameterValidator;
+import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameters;
+import org.apache.iotdb.db.query.udf.api.customizer.strategy.RowByRowAccessStrategy;
+import org.apache.iotdb.db.query.udf.api.exception.UDFException;
+import org.apache.iotdb.db.query.udf.api.exception.UDFInputSeriesDataTypeNotValidException;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.utils.Pair;
+
+import java.io.IOException;
+import java.util.ArrayList;
+
+public abstract class UDTFContinuouslySatisfy implements UDTF {
+  protected Long min;
+  protected Long max;

Review comment:
       For better performance, always try your best to use `long` instead of `Long`. 




-- 
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: reviews-unsubscribe@iotdb.apache.org

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