This is an automated email from the ASF dual-hosted git repository. rong pushed a commit to branch const in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 736dc83204de38c2df712be77f179f8d7c2bdede Author: Steve Yurong Su <[email protected]> AuthorDate: Fri Sep 24 16:38:26 2021 +0800 add 3 functions: const, e and pi --- .../db/query/udf/builtin/BuiltinFunction.java | 3 + .../iotdb/db/query/udf/builtin/UDTFConst.java | 123 +++++++++++++++++++++ .../iotdb/db/query/udf/builtin/UDTFConstE.java | 43 +++++++ .../iotdb/db/query/udf/builtin/UDTFConstPi.java | 43 +++++++ 4 files changed, 212 insertions(+) diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/BuiltinFunction.java b/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/BuiltinFunction.java index ead7350..c8fff1f 100644 --- a/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/BuiltinFunction.java +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/BuiltinFunction.java @@ -21,6 +21,9 @@ package org.apache.iotdb.db.query.udf.builtin; /** All built-in UDFs need to register their function names and classes here. */ public enum BuiltinFunction { + CONST("CONST", UDTFConst.class), + E("E", UDTFConstE.class), + PI("PI", UDTFConstPi.class), SIN("SIN", UDTFSin.class), COS("COS", UDTFCos.class), TAN("TAN", UDTFTan.class), diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFConst.java b/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFConst.java new file mode 100644 index 0000000..a09e8be --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFConst.java @@ -0,0 +1,123 @@ +/* + * 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.builtin; + +import org.apache.iotdb.db.query.udf.api.UDTF; +import org.apache.iotdb.db.query.udf.api.access.Row; +import org.apache.iotdb.db.query.udf.api.collector.PointCollector; +import org.apache.iotdb.db.query.udf.api.customizer.config.UDTFConfigurations; +import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameterValidator; +import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameters; +import org.apache.iotdb.db.query.udf.api.customizer.strategy.RowByRowAccessStrategy; +import org.apache.iotdb.db.query.udf.api.exception.UDFParameterNotValidException; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.utils.Binary; + +import java.util.HashSet; +import java.util.Set; + +public class UDTFConst implements UDTF { + + private static final Set<String> VALID_TYPES = new HashSet<>(); + + static { + VALID_TYPES.add(TSDataType.INT32.name()); + VALID_TYPES.add(TSDataType.INT64.name()); + VALID_TYPES.add(TSDataType.FLOAT.name()); + VALID_TYPES.add(TSDataType.DOUBLE.name()); + VALID_TYPES.add(TSDataType.BOOLEAN.name()); + VALID_TYPES.add(TSDataType.TEXT.name()); + } + + private TSDataType dataType; + + private int intValue; + private long longValue; + private float floatValue; + private double doubleValue; + private boolean booleanValue; + private Binary binaryValue; + + @Override + public void validate(UDFParameterValidator validator) throws UDFParameterNotValidException { + validator + .validateRequiredAttribute("value") + .validateRequiredAttribute("type") + .validate( + type -> VALID_TYPES.contains((String) type), + "The given value type is not supported.", + validator.getParameters().getString("type")); + } + + @Override + public void beforeStart(UDFParameters parameters, UDTFConfigurations configurations) { + dataType = TSDataType.valueOf(parameters.getString("type")); + switch (dataType) { + case INT32: + intValue = Integer.parseInt(parameters.getString("value")); + break; + case INT64: + longValue = Long.parseLong(parameters.getString("value")); + break; + case FLOAT: + floatValue = Float.parseFloat(parameters.getString("value")); + break; + case DOUBLE: + doubleValue = Double.parseDouble(parameters.getString("value")); + break; + case BOOLEAN: + booleanValue = Boolean.parseBoolean(parameters.getString("value")); + break; + case TEXT: + binaryValue = Binary.valueOf(parameters.getString("value")); + break; + default: + throw new UnsupportedOperationException(); + } + + configurations.setAccessStrategy(new RowByRowAccessStrategy()).setOutputDataType(dataType); + } + + @Override + public void transform(Row row, PointCollector collector) throws Exception { + switch (dataType) { + case INT32: + collector.putInt(row.getTime(), intValue); + break; + case INT64: + collector.putLong(row.getTime(), longValue); + break; + case FLOAT: + collector.putFloat(row.getTime(), floatValue); + break; + case DOUBLE: + collector.putDouble(row.getTime(), doubleValue); + break; + case BOOLEAN: + collector.putBoolean(row.getTime(), booleanValue); + break; + case TEXT: + collector.putBinary(row.getTime(), binaryValue); + break; + default: + throw new UnsupportedOperationException(); + } + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFConstE.java b/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFConstE.java new file mode 100644 index 0000000..0e6ee37 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFConstE.java @@ -0,0 +1,43 @@ +/* + * 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.builtin; + +import org.apache.iotdb.db.query.udf.api.UDTF; +import org.apache.iotdb.db.query.udf.api.access.Row; +import org.apache.iotdb.db.query.udf.api.collector.PointCollector; +import org.apache.iotdb.db.query.udf.api.customizer.config.UDTFConfigurations; +import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameters; +import org.apache.iotdb.db.query.udf.api.customizer.strategy.RowByRowAccessStrategy; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +public class UDTFConstE implements UDTF { + + @Override + public void beforeStart(UDFParameters parameters, UDTFConfigurations configurations) { + configurations + .setAccessStrategy(new RowByRowAccessStrategy()) + .setOutputDataType(TSDataType.DOUBLE); + } + + @Override + public void transform(Row row, PointCollector collector) throws Exception { + collector.putDouble(row.getTime(), Math.E); + } +} diff --git a/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFConstPi.java b/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFConstPi.java new file mode 100644 index 0000000..c2724c7 --- /dev/null +++ b/server/src/main/java/org/apache/iotdb/db/query/udf/builtin/UDTFConstPi.java @@ -0,0 +1,43 @@ +/* + * 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.builtin; + +import org.apache.iotdb.db.query.udf.api.UDTF; +import org.apache.iotdb.db.query.udf.api.access.Row; +import org.apache.iotdb.db.query.udf.api.collector.PointCollector; +import org.apache.iotdb.db.query.udf.api.customizer.config.UDTFConfigurations; +import org.apache.iotdb.db.query.udf.api.customizer.parameter.UDFParameters; +import org.apache.iotdb.db.query.udf.api.customizer.strategy.RowByRowAccessStrategy; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; + +public class UDTFConstPi implements UDTF { + + @Override + public void beforeStart(UDFParameters parameters, UDTFConfigurations configurations) { + configurations + .setAccessStrategy(new RowByRowAccessStrategy()) + .setOutputDataType(TSDataType.DOUBLE); + } + + @Override + public void transform(Row row, PointCollector collector) throws Exception { + collector.putDouble(row.getTime(), Math.PI); + } +}
