ericpai commented on a change in pull request #5273:
URL: https://github.com/apache/iotdb/pull/5273#discussion_r835694344



##########
File path: server/src/main/java/org/apache/iotdb/db/qp/sql/IoTDBSqlVisitor.java
##########
@@ -92,15 +92,9 @@
 import org.apache.iotdb.db.query.executor.fill.ValueFill;
 import org.apache.iotdb.db.query.expression.Expression;
 import org.apache.iotdb.db.query.expression.ResultColumn;
-import org.apache.iotdb.db.query.expression.binary.AdditionExpression;
-import org.apache.iotdb.db.query.expression.binary.DivisionExpression;
-import org.apache.iotdb.db.query.expression.binary.ModuloExpression;
-import org.apache.iotdb.db.query.expression.binary.MultiplicationExpression;
-import org.apache.iotdb.db.query.expression.binary.SubtractionExpression;
-import org.apache.iotdb.db.query.expression.unary.ConstantOperand;
-import org.apache.iotdb.db.query.expression.unary.FunctionExpression;
-import org.apache.iotdb.db.query.expression.unary.NegationExpression;
-import org.apache.iotdb.db.query.expression.unary.TimeSeriesOperand;
+import org.apache.iotdb.db.query.expression.binary.*;

Review comment:
       Please do not use wildcard imports.

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/expression/unary/LogicNotExpression.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.expression.unary;
+
+import org.apache.iotdb.db.exception.query.LogicalOptimizeException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.metadata.path.PartialPath;
+import org.apache.iotdb.db.qp.physical.crud.UDTFPlan;
+import org.apache.iotdb.db.qp.utils.WildcardsRemover;
+import org.apache.iotdb.db.query.expression.Expression;
+import org.apache.iotdb.db.query.udf.core.executor.UDTFExecutor;
+import org.apache.iotdb.db.query.udf.core.layer.*;

Review comment:
       Please do not use wildcard imports.

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/BinaryTransformer.java
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.core.transformer;
+
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.query.udf.core.reader.LayerPointReader;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+import java.io.IOException;
+
+public abstract class BinaryTransformer extends Transformer {
+
+  private final LayerPointReader leftPointReader;
+  private final LayerPointReader rightPointReader;
+
+  protected BinaryTransformer(LayerPointReader leftPointReader, 
LayerPointReader rightPointReader) {
+    this.leftPointReader = leftPointReader;
+    this.rightPointReader = rightPointReader;
+  }
+
+  @Override
+  public boolean isConstantPointReader() {
+    return leftPointReader.isConstantPointReader() && 
rightPointReader.isConstantPointReader();
+  }
+
+  @Override
+  protected boolean cacheValue() throws QueryProcessException, IOException {
+    if (!leftPointReader.next() || !rightPointReader.next()) {
+      return false;
+    }
+    if (!cacheTime()) {
+      return false;
+    }
+    if (leftPointReader.isCurrentNull() || rightPointReader.isCurrentNull()) {
+      currentNull = true;
+    } else {
+      switch (getDataType()) {
+        case DOUBLE:
+          cachedDouble =
+              evaluateDouble(
+                  castCurrentValueToDoubleOperand(leftPointReader),
+                  castCurrentValueToDoubleOperand(rightPointReader));
+          break;
+        case BOOLEAN:
+          cachedBoolean =
+              evaluateBoolean(
+                  castCurrentValueToDoubleOperand(leftPointReader),
+                  castCurrentValueToDoubleOperand(rightPointReader));
+      }
+    }
+    leftPointReader.readyForNext();
+    rightPointReader.readyForNext();
+    return true;
+  }
+
+  /**
+   * finds the smallest, unconsumed timestamp that exists in both {@code 
leftPointReader} and {@code
+   * rightPointReader} and then caches the timestamp in {@code cachedTime}.
+   *
+   * @return true if there has a timestamp that meets the requirements
+   */
+  private boolean cacheTime() throws IOException, QueryProcessException {
+    if (leftPointReader.isConstantPointReader() && 
rightPointReader.isConstantPointReader()) {
+      return true;
+    }
+    if (leftPointReader.isConstantPointReader()) {
+      cachedTime = rightPointReader.currentTime();
+      return true;
+    }
+    if (rightPointReader.isConstantPointReader()) {
+      cachedTime = leftPointReader.currentTime();
+      return true;
+    }
+
+    long leftTime = leftPointReader.currentTime();
+    long rightTime = rightPointReader.currentTime();
+
+    while (leftTime != rightTime) {
+      if (leftTime < rightTime) {
+        leftPointReader.readyForNext();
+        if (!leftPointReader.next()) {
+          return false;
+        }
+        leftTime = leftPointReader.currentTime();
+      } else {
+        rightPointReader.readyForNext();
+        if (!rightPointReader.next()) {
+          return false;
+        }
+        rightTime = rightPointReader.currentTime();
+      }
+    }
+
+    // leftTime == rightTime
+    cachedTime = leftTime;
+    return true;
+  }
+
+  protected double evaluateDouble(double leftOperand, double rightOperand) {
+    return 0.0;
+  }
+
+  protected boolean evaluateBoolean(double leftOperand, double rightOperand) {
+    return false;
+  }
+
+  private static double castCurrentValueToDoubleOperand(LayerPointReader 
layerPointReader)
+      throws IOException, QueryProcessException {
+    switch (layerPointReader.getDataType()) {
+      case INT32:
+        return layerPointReader.currentInt();
+      case INT64:
+        return layerPointReader.currentLong();
+      case FLOAT:
+        return layerPointReader.currentFloat();
+      case DOUBLE:
+        return layerPointReader.currentDouble();
+      case BOOLEAN:
+        return layerPointReader.currentBoolean() ? 1.0 : 0.0;
+      default:
+        throw new QueryProcessException(
+            "Unsupported data type: " + 
layerPointReader.getDataType().toString());
+    }
+  }
+
+  @Override
+  public TSDataType getDataType() {

Review comment:
       I think this one should also be abstract as the sub-classes may return 
`DOUBLE` or `BOOLEAN`.

##########
File path: 
server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/BinaryTransformer.java
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.core.transformer;
+
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.query.udf.core.reader.LayerPointReader;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+import java.io.IOException;
+
+public abstract class BinaryTransformer extends Transformer {
+
+  private final LayerPointReader leftPointReader;
+  private final LayerPointReader rightPointReader;
+
+  protected BinaryTransformer(LayerPointReader leftPointReader, 
LayerPointReader rightPointReader) {
+    this.leftPointReader = leftPointReader;
+    this.rightPointReader = rightPointReader;
+  }
+
+  @Override
+  public boolean isConstantPointReader() {
+    return leftPointReader.isConstantPointReader() && 
rightPointReader.isConstantPointReader();
+  }
+
+  @Override
+  protected boolean cacheValue() throws QueryProcessException, IOException {
+    if (!leftPointReader.next() || !rightPointReader.next()) {
+      return false;
+    }
+    if (!cacheTime()) {
+      return false;
+    }
+    if (leftPointReader.isCurrentNull() || rightPointReader.isCurrentNull()) {
+      currentNull = true;
+    } else {
+      switch (getDataType()) {
+        case DOUBLE:
+          cachedDouble =
+              evaluateDouble(
+                  castCurrentValueToDoubleOperand(leftPointReader),
+                  castCurrentValueToDoubleOperand(rightPointReader));
+          break;
+        case BOOLEAN:
+          cachedBoolean =
+              evaluateBoolean(
+                  castCurrentValueToDoubleOperand(leftPointReader),
+                  castCurrentValueToDoubleOperand(rightPointReader));
+      }
+    }
+    leftPointReader.readyForNext();
+    rightPointReader.readyForNext();
+    return true;
+  }
+
+  /**
+   * finds the smallest, unconsumed timestamp that exists in both {@code 
leftPointReader} and {@code
+   * rightPointReader} and then caches the timestamp in {@code cachedTime}.
+   *
+   * @return true if there has a timestamp that meets the requirements
+   */
+  private boolean cacheTime() throws IOException, QueryProcessException {
+    if (leftPointReader.isConstantPointReader() && 
rightPointReader.isConstantPointReader()) {
+      return true;
+    }
+    if (leftPointReader.isConstantPointReader()) {
+      cachedTime = rightPointReader.currentTime();
+      return true;
+    }
+    if (rightPointReader.isConstantPointReader()) {
+      cachedTime = leftPointReader.currentTime();
+      return true;
+    }
+
+    long leftTime = leftPointReader.currentTime();
+    long rightTime = rightPointReader.currentTime();
+
+    while (leftTime != rightTime) {
+      if (leftTime < rightTime) {
+        leftPointReader.readyForNext();
+        if (!leftPointReader.next()) {
+          return false;
+        }
+        leftTime = leftPointReader.currentTime();
+      } else {
+        rightPointReader.readyForNext();
+        if (!rightPointReader.next()) {
+          return false;
+        }
+        rightTime = rightPointReader.currentTime();
+      }
+    }
+
+    // leftTime == rightTime
+    cachedTime = leftTime;
+    return true;
+  }
+
+  protected double evaluateDouble(double leftOperand, double rightOperand) {
+    return 0.0;
+  }
+
+  protected boolean evaluateBoolean(double leftOperand, double rightOperand) {

Review comment:
       Could `evaluateBoolean` accept two boolean parameters instead of double?




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