lancelly commented on code in PR #9310: URL: https://github.com/apache/iotdb/pull/9310#discussion_r1136545833
########## integration-test/src/test/java/org/apache/iotdb/db/it/builtinfunction/scalar/IoTDBRoundFunctionIT.java: ########## @@ -0,0 +1,196 @@ +/* + * 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.it.builtinfunction.scalar; + +import org.apache.iotdb.it.env.EnvFactory; +import org.apache.iotdb.it.framework.IoTDBTestRunner; +import org.apache.iotdb.itbase.category.ClusterIT; +import org.apache.iotdb.itbase.category.LocalStandaloneIT; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import static org.apache.iotdb.db.it.utils.TestUtils.assertTestFail; +import static org.apache.iotdb.db.it.utils.TestUtils.prepareData; +import static org.apache.iotdb.db.it.utils.TestUtils.resultSetEqualTest; +import static org.apache.iotdb.itbase.constant.TestConstant.TIMESTAMP_STR; + +@RunWith(IoTDBTestRunner.class) +@Category({LocalStandaloneIT.class, ClusterIT.class}) +public class IoTDBRoundFunctionIT { Review Comment: Perhaps we need to add more test cases: 1. testWithUDF 2. More values of places, places could be 2,6...etc ########## server/src/main/java/org/apache/iotdb/db/mpp/transformation/dag/transformer/unary/scalar/RoundFunctionTransformer.java: ########## @@ -0,0 +1,72 @@ +/* + * 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.mpp.transformation.dag.transformer.unary.scalar; + +import org.apache.iotdb.db.exception.query.QueryProcessException; +import org.apache.iotdb.db.mpp.transformation.api.LayerPointReader; +import org.apache.iotdb.db.mpp.transformation.dag.transformer.unary.UnaryTransformer; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +import java.io.IOException; + +public class RoundFunctionTransformer extends UnaryTransformer { + private final TSDataType targetDataType; + + protected int places; + + public RoundFunctionTransformer( + LayerPointReader layerPointReader, TSDataType targetDataType, int places) { + super(layerPointReader); + this.targetDataType = targetDataType; + this.places = places; + } + + @Override + public TSDataType getDataType() { + return targetDataType; + } + + @Override + protected void transformAndCache() throws QueryProcessException, IOException { + switch (layerPointReaderDataType) { + case INT32: + cachedDouble = + Math.rint(layerPointReader.currentInt() * Math.pow(10, places)) / Math.pow(10, places); Review Comment: This method could be wrong in some cases. Maybe we should discuss whether we need to use BigDecimal or just stay the same with Mr.Tien @JackieTien97 ########## server/src/main/java/org/apache/iotdb/db/mpp/transformation/dag/column/unary/scalar/RoundFunctionColumnTransformer.java: ########## @@ -0,0 +1,70 @@ +/* + * 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.mpp.transformation.dag.column.unary.scalar; + +import org.apache.iotdb.db.mpp.transformation.dag.column.ColumnTransformer; +import org.apache.iotdb.db.mpp.transformation.dag.column.unary.UnaryColumnTransformer; +import org.apache.iotdb.tsfile.read.common.block.column.Column; +import org.apache.iotdb.tsfile.read.common.block.column.ColumnBuilder; +import org.apache.iotdb.tsfile.read.common.type.Type; +import org.apache.iotdb.tsfile.read.common.type.TypeEnum; + +public class RoundFunctionColumnTransformer extends UnaryColumnTransformer { + + protected int places; + + public RoundFunctionColumnTransformer( + Type returnType, ColumnTransformer childColumnTransformer, int places) { + super(returnType, childColumnTransformer); + this.places = places; + } + + @Override + protected void doTransform(Column column, ColumnBuilder columnBuilder) { + TypeEnum sourceType = childColumnTransformer.getType().getTypeEnum(); + for (int i = 0, n = column.getPositionCount(); i < n; i++) { + if (!column.isNull(i)) { + switch (sourceType) { + case INT32: + columnBuilder.writeDouble(column.getInt(i)); + break; + case INT64: + columnBuilder.writeDouble(column.getLong(i)); Review Comment: Why not Math.rint(column.getInt(i) * Math.pow(10, places)) / Math.pow(10, places)); ########## server/src/main/java/org/apache/iotdb/db/mpp/plan/expression/multi/builtin/helper/RoundHelper.java: ########## @@ -0,0 +1,90 @@ +/* + * 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.mpp.plan.expression.multi.builtin.helper; + +import org.apache.iotdb.db.exception.sql.SemanticException; +import org.apache.iotdb.db.mpp.plan.expression.Expression; +import org.apache.iotdb.db.mpp.plan.expression.multi.FunctionExpression; +import org.apache.iotdb.db.mpp.plan.expression.multi.builtin.BuiltInScalarFunctionHelper; +import org.apache.iotdb.db.mpp.transformation.api.LayerPointReader; +import org.apache.iotdb.db.mpp.transformation.dag.column.ColumnTransformer; +import org.apache.iotdb.db.mpp.transformation.dag.column.unary.scalar.RoundFunctionColumnTransformer; +import org.apache.iotdb.db.mpp.transformation.dag.transformer.Transformer; +import org.apache.iotdb.db.mpp.transformation.dag.transformer.unary.scalar.RoundFunctionTransformer; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.read.common.type.TypeFactory; + +import java.util.List; + +import static org.apache.iotdb.db.mpp.plan.parser.ASTVisitor.checkFunctionExpressionInputSize; + +public class RoundHelper implements BuiltInScalarFunctionHelper { + @Override + public void checkBuiltInScalarFunctionInputSize(FunctionExpression functionExpression) + throws SemanticException { + checkFunctionExpressionInputSize( + functionExpression.getExpressionString(), functionExpression.getExpressions().size(), 1, 2); + } + + @Override + public void checkBuiltInScalarFunctionInputDataType(TSDataType tsDataType) + throws SemanticException { + if (tsDataType.isNumeric()) { + return; + } + throw new SemanticException( + "Input series of Scalar function [ROUND] only supports numeric data types [INT32, INT64, FLOAT, DOUBLE]"); + } + + @Override + public TSDataType getBuiltInScalarFunctionReturnType(FunctionExpression functionExpression) { + return TSDataType.DOUBLE; + } + + @Override + public ColumnTransformer getBuiltInScalarFunctionColumnTransformer( + FunctionExpression expression, ColumnTransformer columnTransformer) { + List<Expression> expressions = expression.getExpressions(); + if (expressions.size() == 1) { + return new RoundFunctionColumnTransformer( + TypeFactory.getType(this.getBuiltInScalarFunctionReturnType(expression)), + columnTransformer, + 0); + } + return new RoundFunctionColumnTransformer( + TypeFactory.getType(this.getBuiltInScalarFunctionReturnType(expression)), + columnTransformer, + Integer.parseInt(expressions.get(1).getExpressionString())); + } + + @Override + public Transformer getBuiltInScalarFunctionTransformer( + FunctionExpression expression, LayerPointReader layerPointReader) { + List<Expression> expressions = expression.getExpressions(); + if (expressions.size() == 1) { + return new RoundFunctionTransformer( + layerPointReader, this.getBuiltInScalarFunctionReturnType(expression), 0); + } + return new RoundFunctionTransformer( + layerPointReader, + this.getBuiltInScalarFunctionReturnType(expression), + Integer.parseInt(expressions.get(1).getExpressionString())); Review Comment: It is better we use attributeValue to represent places rather than using expression. -- 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]
