JackieTien97 commented on code in PR #9226: URL: https://github.com/apache/iotdb/pull/9226#discussion_r1126415180
########## server/src/main/java/org/apache/iotdb/db/mpp/transformation/dag/column/unary/scalar/CastFunctionColumnTransformer.java: ########## @@ -0,0 +1,227 @@ +/* + * 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.utils.Binary; + +public class CastFunctionColumnTransformer extends UnaryColumnTransformer { Review Comment: use freemarker to gen the code for different data types combinations. ########## server/src/main/java/org/apache/iotdb/db/mpp/transformation/dag/column/unary/scalar/CastFunctionColumnTransformer.java: ########## @@ -0,0 +1,227 @@ +/* + * 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.utils.Binary; + +public class CastFunctionColumnTransformer extends UnaryColumnTransformer { + + public CastFunctionColumnTransformer(Type returnType, ColumnTransformer childColumnTransformer) { + super(returnType, childColumnTransformer); + } + + @Override + protected void doTransform(Column column, ColumnBuilder columnBuilder) { + for (int i = 0, n = column.getPositionCount(); i < n; i++) { + if (!column.isNull(i)) { + switch (childColumnTransformer.getType().getTypeEnum()) { + case INT32: + cast(columnBuilder, childColumnTransformer.getType().getInt(column, i)); + break; + case INT64: + cast(columnBuilder, childColumnTransformer.getType().getLong(column, i)); + break; + case FLOAT: + cast(columnBuilder, childColumnTransformer.getType().getFloat(column, i)); + break; + case DOUBLE: + cast(columnBuilder, childColumnTransformer.getType().getDouble(column, i)); + break; + case BOOLEAN: + cast(columnBuilder, childColumnTransformer.getType().getBoolean(column, i)); + break; + case BINARY: + cast(columnBuilder, childColumnTransformer.getType().getBinary(column, i)); + break; + default: + throw new UnsupportedOperationException( + String.format( + "Unsupported source dataType: %s", + childColumnTransformer.getType().getTypeEnum())); + } + } else { + columnBuilder.appendNull(); + } + } + } + + private void cast(ColumnBuilder columnBuilder, int value) { + switch (returnType.getTypeEnum()) { + case INT32: + returnType.writeInt(columnBuilder, value); + break; + case INT64: + returnType.writeLong(columnBuilder, value); + break; + case FLOAT: + returnType.writeFloat(columnBuilder, value); + break; + case DOUBLE: + returnType.writeDouble(columnBuilder, value); + break; + case BOOLEAN: + returnType.writeBoolean(columnBuilder, value != 0); + break; + case BINARY: + returnType.writeBinary(columnBuilder, Binary.valueOf(String.valueOf(value))); + break; + default: + throw new UnsupportedOperationException( + String.format("Unsupported target dataType: %s", returnType.getTypeEnum())); + } + } + + private void cast(ColumnBuilder columnBuilder, long value) { + switch (returnType.getTypeEnum()) { + case INT32: + returnType.writeInt(columnBuilder, (int) value); + break; + case INT64: + returnType.writeLong(columnBuilder, value); + break; + case FLOAT: + returnType.writeFloat(columnBuilder, value); + break; + case DOUBLE: + returnType.writeDouble(columnBuilder, value); + break; + case BOOLEAN: + returnType.writeBoolean(columnBuilder, value != 0L); + break; + case BINARY: + returnType.writeBinary(columnBuilder, Binary.valueOf(String.valueOf(value))); + break; + default: + throw new UnsupportedOperationException( + String.format("Unsupported target dataType: %s", returnType.getTypeEnum())); + } + } + + private void cast(ColumnBuilder columnBuilder, float value) { + switch (returnType.getTypeEnum()) { + case INT32: + returnType.writeInt(columnBuilder, (int) value); + break; + case INT64: + returnType.writeLong(columnBuilder, (long) value); + break; + case FLOAT: + returnType.writeFloat(columnBuilder, value); + break; + case DOUBLE: + returnType.writeDouble(columnBuilder, value); + break; + case BOOLEAN: + returnType.writeBoolean(columnBuilder, value != 0.0f); + break; + case BINARY: + returnType.writeBinary(columnBuilder, Binary.valueOf(String.valueOf(value))); + break; + default: + throw new UnsupportedOperationException( + String.format("Unsupported target dataType: %s", returnType.getTypeEnum())); + } + } + + private void cast(ColumnBuilder columnBuilder, double value) { + switch (returnType.getTypeEnum()) { + case INT32: + returnType.writeInt(columnBuilder, (int) value); + break; + case INT64: + returnType.writeLong(columnBuilder, (long) value); + break; + case FLOAT: + returnType.writeFloat(columnBuilder, (float) value); + break; + case DOUBLE: + returnType.writeDouble(columnBuilder, value); + break; + case BOOLEAN: + returnType.writeBoolean(columnBuilder, value != 0.0); + break; + case BINARY: + returnType.writeBinary(columnBuilder, Binary.valueOf(String.valueOf(value))); + break; + default: + throw new UnsupportedOperationException( + String.format("Unsupported target dataType: %s", returnType.getTypeEnum())); + } + } + + private void cast(ColumnBuilder columnBuilder, boolean value) { + switch (returnType.getTypeEnum()) { + case INT32: + returnType.writeInt(columnBuilder, value ? 1 : 0); + break; + case INT64: + returnType.writeLong(columnBuilder, value ? 1L : 0); + break; + case FLOAT: + returnType.writeFloat(columnBuilder, value ? 1.0f : 0); + break; + case DOUBLE: + returnType.writeDouble(columnBuilder, value ? 1.0 : 0); + break; + case BOOLEAN: + returnType.writeBoolean(columnBuilder, value); + break; + case BINARY: + returnType.writeBinary(columnBuilder, Binary.valueOf(String.valueOf(value))); + break; + default: + throw new UnsupportedOperationException( + String.format("Unsupported target dataType: %s", returnType.getTypeEnum())); + } + } + + private void cast(ColumnBuilder columnBuilder, Binary value) { + String stringValue = value.getStringValue(); + switch (returnType.getTypeEnum()) { + case INT32: + returnType.writeInt(columnBuilder, (int) Double.parseDouble(stringValue)); Review Comment: I found that if we directly use Integer.parseInt for string value `1.1`, it will throw exception, so I wonder that how pg or mysql(standard sql) deal with this case? ########## server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/ExpressionAnalyzer.java: ########## @@ -1324,8 +1324,8 @@ public static boolean isDeviceViewNeedSpecialProcess(Expression expression) { } else if (expression instanceof UnaryExpression) { return isDeviceViewNeedSpecialProcess(((UnaryExpression) expression).getExpression()); } else if (expression instanceof FunctionExpression) { - if (((FunctionExpression) expression).isBuiltInFunction() - && BuiltinFunction.DEVICE_VIEW_SPECIAL_PROCESS_FUNCTIONS.contains( + if (((FunctionExpression) expression).isBuiltInScalarFunction() Review Comment: Previously, we named it as BuiltInScalarFunction, but I've forgot why Weihao Li changed it, you need to discuss with him @Wei-hao-Li -- 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]
