This is an automated email from the ASF dual-hosted git repository.
dockerzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git
The following commit(s) were added to refs/heads/master by this push:
new a26a3354eb [INLONG-10613][SDK] Transform SQL support arithmetic
functions(Including ceil, floor, sin and sinh) (#10760)
a26a3354eb is described below
commit a26a3354ebd435a283bf83ad0c62b6b494296f3a
Author: LeeWY <[email protected]>
AuthorDate: Wed Aug 7 20:50:05 2024 +0800
[INLONG-10613][SDK] Transform SQL support arithmetic functions(Including
ceil, floor, sin and sinh) (#10760)
---
.../transform/process/function/CeilFunction.java | 57 ++++++++++++++++
.../transform/process/function/FloorFunction.java | 57 ++++++++++++++++
.../transform/process/function/SinFunction.java | 57 ++++++++++++++++
.../transform/process/function/SinhFunction.java | 57 ++++++++++++++++
.../transform/process/operator/OperatorTools.java | 8 +++
.../TestTransformArithmeticFunctionsProcessor.java | 76 ++++++++++++++++++++++
6 files changed, 312 insertions(+)
diff --git
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/CeilFunction.java
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/CeilFunction.java
new file mode 100644
index 0000000000..adb472ae6b
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/CeilFunction.java
@@ -0,0 +1,57 @@
+/*
+ * 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.inlong.sdk.transform.process.function;
+
+import org.apache.inlong.sdk.transform.decode.SourceData;
+import org.apache.inlong.sdk.transform.process.Context;
+import org.apache.inlong.sdk.transform.process.operator.OperatorTools;
+import org.apache.inlong.sdk.transform.process.parser.ValueParser;
+
+import net.sf.jsqlparser.expression.Function;
+
+import java.math.BigDecimal;
+
+/**
+ * CeilFunction
+ * description: ceil(numeric)--rounds numeric up, and returns the smallest
number that is greater than or equal to numeric
+ */
+public class CeilFunction implements ValueParser {
+
+ private ValueParser numberParser;
+
+ /**
+ * Constructor
+ * @param expr
+ */
+ public CeilFunction(Function expr) {
+ numberParser =
OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
+ }
+
+ /**
+ * parse
+ * @param sourceData
+ * @param rowIndex
+ * @return
+ */
+ @Override
+ public Object parse(SourceData sourceData, int rowIndex, Context context) {
+ Object numberObj = numberParser.parse(sourceData, rowIndex, context);
+ BigDecimal numberValue = OperatorTools.parseBigDecimal(numberObj);
+ return Math.ceil(numberValue.doubleValue());
+ }
+}
diff --git
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/FloorFunction.java
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/FloorFunction.java
new file mode 100644
index 0000000000..9d5322570b
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/FloorFunction.java
@@ -0,0 +1,57 @@
+/*
+ * 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.inlong.sdk.transform.process.function;
+
+import org.apache.inlong.sdk.transform.decode.SourceData;
+import org.apache.inlong.sdk.transform.process.Context;
+import org.apache.inlong.sdk.transform.process.operator.OperatorTools;
+import org.apache.inlong.sdk.transform.process.parser.ValueParser;
+
+import net.sf.jsqlparser.expression.Function;
+
+import java.math.BigDecimal;
+
+/**
+ * FloorFunction
+ * description: floor(numeric)--rounds numeric down, and returns the largest
number that is less than or equal to numeric
+ */
+public class FloorFunction implements ValueParser {
+
+ private ValueParser numberParser;
+
+ /**
+ * Constructor
+ * @param expr
+ */
+ public FloorFunction(Function expr) {
+ numberParser =
OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
+ }
+
+ /**
+ * parse
+ * @param sourceData
+ * @param rowIndex
+ * @return
+ */
+ @Override
+ public Object parse(SourceData sourceData, int rowIndex, Context context) {
+ Object numberObj = numberParser.parse(sourceData, rowIndex, context);
+ BigDecimal numberValue = OperatorTools.parseBigDecimal(numberObj);
+ return Math.floor(numberValue.doubleValue());
+ }
+}
diff --git
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/SinFunction.java
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/SinFunction.java
new file mode 100644
index 0000000000..5f97ccc119
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/SinFunction.java
@@ -0,0 +1,57 @@
+/*
+ * 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.inlong.sdk.transform.process.function;
+
+import org.apache.inlong.sdk.transform.decode.SourceData;
+import org.apache.inlong.sdk.transform.process.Context;
+import org.apache.inlong.sdk.transform.process.operator.OperatorTools;
+import org.apache.inlong.sdk.transform.process.parser.ValueParser;
+
+import net.sf.jsqlparser.expression.Function;
+
+import java.math.BigDecimal;
+
+/**
+ * SinFunction
+ * description: sin(numeric)--returns the sine of numeric
+ */
+public class SinFunction implements ValueParser {
+
+ private ValueParser numberParser;
+
+ /**
+ * Constructor
+ * @param expr
+ */
+ public SinFunction(Function expr) {
+ numberParser =
OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
+ }
+
+ /**
+ * parse
+ * @param sourceData
+ * @param rowIndex
+ * @return
+ */
+ @Override
+ public Object parse(SourceData sourceData, int rowIndex, Context context) {
+ Object numberObj = numberParser.parse(sourceData, rowIndex, context);
+ BigDecimal numberValue = OperatorTools.parseBigDecimal(numberObj);
+ return Math.sin(numberValue.doubleValue());
+ }
+}
diff --git
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/SinhFunction.java
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/SinhFunction.java
new file mode 100644
index 0000000000..7eb947aeb2
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/SinhFunction.java
@@ -0,0 +1,57 @@
+/*
+ * 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.inlong.sdk.transform.process.function;
+
+import org.apache.inlong.sdk.transform.decode.SourceData;
+import org.apache.inlong.sdk.transform.process.Context;
+import org.apache.inlong.sdk.transform.process.operator.OperatorTools;
+import org.apache.inlong.sdk.transform.process.parser.ValueParser;
+
+import net.sf.jsqlparser.expression.Function;
+
+import java.math.BigDecimal;
+
+/**
+ * SinhFunction
+ * description: sinh(numeric)--returns the hyperbolic sine of numeric
+ */
+public class SinhFunction implements ValueParser {
+
+ private ValueParser numberParser;
+
+ /**
+ * Constructor
+ * @param expr
+ */
+ public SinhFunction(Function expr) {
+ numberParser =
OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
+ }
+
+ /**
+ * parse
+ * @param sourceData
+ * @param rowIndex
+ * @return
+ */
+ @Override
+ public Object parse(SourceData sourceData, int rowIndex, Context context) {
+ Object numberObj = numberParser.parse(sourceData, rowIndex, context);
+ BigDecimal numberValue = OperatorTools.parseBigDecimal(numberObj);
+ return Math.sinh(numberValue.doubleValue());
+ }
+}
diff --git
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java
index bc97d11148..dbfa4b93c8 100644
---
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java
+++
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java
@@ -18,9 +18,11 @@
package org.apache.inlong.sdk.transform.process.operator;
import org.apache.inlong.sdk.transform.process.function.AbsFunction;
+import org.apache.inlong.sdk.transform.process.function.CeilFunction;
import org.apache.inlong.sdk.transform.process.function.ConcatFunction;
import org.apache.inlong.sdk.transform.process.function.DateFormatFunction;
import org.apache.inlong.sdk.transform.process.function.ExpFunction;
+import org.apache.inlong.sdk.transform.process.function.FloorFunction;
import org.apache.inlong.sdk.transform.process.function.LnFunction;
import org.apache.inlong.sdk.transform.process.function.LocateFunction;
import org.apache.inlong.sdk.transform.process.function.Log10Function;
@@ -28,6 +30,8 @@ import
org.apache.inlong.sdk.transform.process.function.Log2Function;
import org.apache.inlong.sdk.transform.process.function.LogFunction;
import org.apache.inlong.sdk.transform.process.function.NowFunction;
import org.apache.inlong.sdk.transform.process.function.PowerFunction;
+import org.apache.inlong.sdk.transform.process.function.SinFunction;
+import org.apache.inlong.sdk.transform.process.function.SinhFunction;
import org.apache.inlong.sdk.transform.process.function.SqrtFunction;
import org.apache.inlong.sdk.transform.process.function.SubstringFunction;
import org.apache.inlong.sdk.transform.process.function.ToDateFunction;
@@ -93,6 +97,10 @@ public class OperatorTools {
functionMap.put("locate", LocateFunction::new);
functionMap.put("to_date", ToDateFunction::new);
functionMap.put("date_format", DateFormatFunction::new);
+ functionMap.put("ceil", CeilFunction::new);
+ functionMap.put("floor", FloorFunction::new);
+ functionMap.put("sin", SinFunction::new);
+ functionMap.put("sinh", SinhFunction::new);
}
public static ExpressionOperator buildOperator(Expression expr) {
diff --git
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java
index 3bf58495a9..b8431460e5 100644
---
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java
+++
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java
@@ -205,4 +205,80 @@ public class TestTransformArithmeticFunctionsProcessor {
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=7.38905609893065");
}
+
+ @Test
+ public void testCeilFunction() throws Exception {
+ String transformSql = "select ceil(numeric1) from source";
+ TransformConfig config = new TransformConfig(transformSql);
+ // case1: ceil(1.23)
+ TransformProcessor<String, String> processor = TransformProcessor
+ .create(config,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+ List<String> output1 = processor.transform("1.23|4|6|8", new
HashMap<>());
+ Assert.assertEquals(1, output1.size());
+ Assert.assertEquals(output1.get(0), "result=2.0");
+ // case2: ceil(3)
+ List<String> output2 = processor.transform("3|-2|6|8", new
HashMap<>());
+ Assert.assertEquals(1, output2.size());
+ Assert.assertEquals(output2.get(0), "result=3.0");
+ // case3: ceil(-5.67)
+ List<String> output3 = processor.transform("-5.67|0.5|6|8", new
HashMap<>());
+ Assert.assertEquals(1, output3.size());
+ Assert.assertEquals(output3.get(0), "result=-5.0");
+ }
+
+ @Test
+ public void testFloorFunction() throws Exception {
+ String transformSql = "select floor(numeric1) from source";
+ TransformConfig config = new TransformConfig(transformSql);
+ // case1: floor(1.23)
+ TransformProcessor<String, String> processor = TransformProcessor
+ .create(config,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+ List<String> output1 = processor.transform("1.23|4|6|8", new
HashMap<>());
+ Assert.assertEquals(1, output1.size());
+ Assert.assertEquals(output1.get(0), "result=1.0");
+ // case2: floor(3)
+ List<String> output2 = processor.transform("3|-2|6|8", new
HashMap<>());
+ Assert.assertEquals(1, output2.size());
+ Assert.assertEquals(output2.get(0), "result=3.0");
+ // case3: floor(-5.67)
+ List<String> output3 = processor.transform("-5.67|0.5|6|8", new
HashMap<>());
+ Assert.assertEquals(1, output3.size());
+ Assert.assertEquals(output3.get(0), "result=-6.0");
+ }
+
+ @Test
+ public void testSinFunction() throws Exception {
+ String transformSql = "select sin(numeric1) from source";
+ TransformConfig config = new TransformConfig(transformSql);
+ TransformProcessor<String, String> processor = TransformProcessor
+ .create(config,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+ // case: sin(0)
+ List<String> output1 = processor.transform("0|4|6|8", new HashMap<>());
+ Assert.assertEquals(1, output1.size());
+ Assert.assertEquals(output1.get(0), "result=0.0");
+ }
+
+ @Test
+ public void testSinhFunction() throws Exception {
+ String transformSql = "select sinh(numeric1) from source";
+ TransformConfig config = new TransformConfig(transformSql);
+ TransformProcessor<String, String> processor = TransformProcessor
+ .create(config,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+ // case1: sinh(0)
+ List<String> output1 = processor.transform("0|4|6|8", new HashMap<>());
+ Assert.assertEquals(1, output1.size());
+ Assert.assertEquals(output1.get(0), "result=0.0");
+ // case2: sinh(1)
+ List<String> output2 = processor.transform("1|4|6|8", new HashMap<>());
+ Assert.assertEquals(1, output2.size());
+ Assert.assertEquals(output2.get(0), "result=1.1752011936438014");
+ // case3: sinh(2)
+ List<String> output3 = processor.transform("2|4|6|8", new HashMap<>());
+ Assert.assertEquals(1, output3.size());
+ Assert.assertEquals(output3.get(0), "result=3.626860407847019");
+ }
}