From 7701595a372af8c944080e6930886deec4e01f9d Mon Sep 17 00:00:00 2001 From: Zkplo <87751516+Zkplo@users.noreply.github.com> Date: Wed, 28 Aug 2024 11:04:57 +0800 Subject: [PATCH] [INLONG-10866][SDK] Transform SQL support sign function (#10924) --- .../process/function/SignFunction.java | 53 +++++++++++++++++++ .../process/operator/OperatorTools.java | 2 + ...TransformArithmeticFunctionsProcessor.java | 21 ++++++++ 3 files changed, 76 insertions(+) create mode 100644 inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/SignFunction.java diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/SignFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/SignFunction.java new file mode 100644 index 00000000000..792c8baf267 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/SignFunction.java @@ -0,0 +1,53 @@ +/* + * 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; + +/** + * SignFunction + * description: sign(x): Return the sign of x, where x is a negative number, 0, and positive numbers return -1, 0, and 1, respectively + */ +public class SignFunction implements ValueParser { + + private ValueParser numberParser; + + public SignFunction(Function expr) { + numberParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + Object numberObj = numberParser.parse(sourceData, rowIndex, context); + BigDecimal numberValue = OperatorTools.parseBigDecimal(numberObj); + double value = numberValue.doubleValue(); + if (value > 0) { + return 1; + } else if (value < 0) { + return -1; + } + return 0; + } +} 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 bb6b5307053..a0b1e0d6211 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 @@ -48,6 +48,7 @@ import org.apache.inlong.sdk.transform.process.function.ReverseFunction; import org.apache.inlong.sdk.transform.process.function.RightFunction; import org.apache.inlong.sdk.transform.process.function.RoundFunction; +import org.apache.inlong.sdk.transform.process.function.SignFunction; 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; @@ -135,6 +136,7 @@ public class OperatorTools { functionMap.put("reverse", ReverseFunction::new); functionMap.put("substring", SubstringFunction::new); functionMap.put("trim", TrimFunction::new); + functionMap.put("sign", SignFunction::new); functionMap.put("replicate", ReplicateFunction::new); functionMap.put("locate", LocateFunction::new); functionMap.put("to_date", ToDateFunction::new); 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 08a2430e2d0..b1c2232f15b 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 @@ -56,6 +56,27 @@ public class TestTransformArithmeticFunctionsProcessor { kvSink = new KvSinkInfo("UTF-8", dstFields); } + @Test + public void testSignFunction() throws Exception { + String transformSql = "select sign(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: sign(3.14159265358979323846) + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|4|6|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1"); + // case2: sign(-3.5) + List output2 = processor.transform("-3.5|4|6|8"); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=-1"); + // case3: sign(0) + List output3 = processor.transform("0|4|6|8"); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=0"); + } + @Test public void testModuloFunction() throws Exception { String transformFunctionSql = "select mod(numeric1,100) from source";