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



##########
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:
       for compare expression like `a>b`, `a` `b` are both double. Only in 
logic `and` and `or`, rightOperand and rightOperand are boolean




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