This is an automated email from the ASF dual-hosted git repository. rong pushed a commit to branch iotdb-1400 in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 0b51d0cee2d203d5fd10661fa3e49f2fdd9e926d Author: Steve Yurong Su <[email protected]> AuthorDate: Thu May 27 17:07:35 2021 +0800 ArithmeticBinaryTransformer and its subclasses --- .../transformer/ArithmeticAdditionTransformer.java | 35 +++++++ .../transformer/ArithmeticBinaryTransformer.java | 109 +++++++++++++++++++++ .../transformer/ArithmeticDivisionTransformer.java | 35 +++++++ .../transformer/ArithmeticModuloTransformer.java | 35 +++++++ .../ArithmeticMultiplicationTransformer.java | 35 +++++++ .../ArithmeticSubtractionTransformer.java | 35 +++++++ 6 files changed, 284 insertions(+) diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticAdditionTransformer.java b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticAdditionTransformer.java new file mode 100644 index 0000000..15aa3cc --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticAdditionTransformer.java @@ -0,0 +1,35 @@ +/* + * 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.query.udf.core.reader.LayerPointReader; + +public class ArithmeticAdditionTransformer extends ArithmeticBinaryTransformer { + + protected ArithmeticAdditionTransformer( + LayerPointReader leftPointReader, LayerPointReader rightPointReader) { + super(leftPointReader, rightPointReader); + } + + @Override + protected double evaluate(double leftOperand, double rightOperand) { + return leftOperand + rightOperand; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticBinaryTransformer.java b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticBinaryTransformer.java new file mode 100644 index 0000000..a1e52b8 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticBinaryTransformer.java @@ -0,0 +1,109 @@ +/* + * 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.exception.write.UnSupportedDataTypeException; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +import java.io.IOException; + +public abstract class ArithmeticBinaryTransformer extends Transformer { + + private final LayerPointReader leftPointReader; + private final LayerPointReader rightPointReader; + + protected ArithmeticBinaryTransformer( + LayerPointReader leftPointReader, LayerPointReader rightPointReader) { + this.leftPointReader = leftPointReader; + this.rightPointReader = rightPointReader; + } + + @Override + protected boolean cacheValue() throws QueryProcessException, IOException { + if (!leftPointReader.next() || !rightPointReader.next()) { + return false; + } + if (!cacheTime()) { + return false; + } + evaluate( + castCurrentValueToDoubleOperand(leftPointReader), + castCurrentValueToDoubleOperand(rightPointReader)); + leftPointReader.readyForNext(); + rightPointReader.readyForNext(); + return true; + } + + /** + * Find the smallest, unconsumed timestamp that exists in both {@code leftPointReader} and {@code + * rightPointReader} and then cache it in {@code cachedTime}. + * + * @return true if there has a timestamp that meets the requirements + */ + private boolean cacheTime() throws IOException, QueryProcessException { + 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 abstract double evaluate(double leftOperand, double rightOperand); + + private static double castCurrentValueToDoubleOperand(LayerPointReader layerPointReader) + throws IOException { + switch (layerPointReader.getDataType()) { + case INT32: + return layerPointReader.currentInt(); + case INT64: + return layerPointReader.currentLong(); + case FLOAT: + return layerPointReader.currentFloat(); + case DOUBLE: + return layerPointReader.currentDouble(); + default: + throw new UnSupportedDataTypeException(layerPointReader.toString()); + } + } + + @Override + public TSDataType getDataType() { + return TSDataType.DOUBLE; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticDivisionTransformer.java b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticDivisionTransformer.java new file mode 100644 index 0000000..c4bfd28 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticDivisionTransformer.java @@ -0,0 +1,35 @@ +/* + * 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.query.udf.core.reader.LayerPointReader; + +public class ArithmeticDivisionTransformer extends ArithmeticBinaryTransformer { + + protected ArithmeticDivisionTransformer( + LayerPointReader leftPointReader, LayerPointReader rightPointReader) { + super(leftPointReader, rightPointReader); + } + + @Override + protected double evaluate(double leftOperand, double rightOperand) { + return leftOperand / rightOperand; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticModuloTransformer.java b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticModuloTransformer.java new file mode 100644 index 0000000..9a2ca0d --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticModuloTransformer.java @@ -0,0 +1,35 @@ +/* + * 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.query.udf.core.reader.LayerPointReader; + +public class ArithmeticModuloTransformer extends ArithmeticBinaryTransformer { + + protected ArithmeticModuloTransformer( + LayerPointReader leftPointReader, LayerPointReader rightPointReader) { + super(leftPointReader, rightPointReader); + } + + @Override + protected double evaluate(double leftOperand, double rightOperand) { + return leftOperand % rightOperand; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticMultiplicationTransformer.java b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticMultiplicationTransformer.java new file mode 100644 index 0000000..2298767 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticMultiplicationTransformer.java @@ -0,0 +1,35 @@ +/* + * 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.query.udf.core.reader.LayerPointReader; + +public class ArithmeticMultiplicationTransformer extends ArithmeticBinaryTransformer { + + protected ArithmeticMultiplicationTransformer( + LayerPointReader leftPointReader, LayerPointReader rightPointReader) { + super(leftPointReader, rightPointReader); + } + + @Override + protected double evaluate(double leftOperand, double rightOperand) { + return leftOperand * rightOperand; + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticSubtractionTransformer.java b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticSubtractionTransformer.java new file mode 100644 index 0000000..4ccde7a --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/core/transformer/ArithmeticSubtractionTransformer.java @@ -0,0 +1,35 @@ +/* + * 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.query.udf.core.reader.LayerPointReader; + +public class ArithmeticSubtractionTransformer extends ArithmeticBinaryTransformer { + + protected ArithmeticSubtractionTransformer( + LayerPointReader leftPointReader, LayerPointReader rightPointReader) { + super(leftPointReader, rightPointReader); + } + + @Override + protected double evaluate(double leftOperand, double rightOperand) { + return leftOperand - rightOperand; + } +}
