This is an automated email from the ASF dual-hosted git repository.

aloyszhang 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 f336100ccd [INLONG-11263][SDK] Transform SQL supports "lcm" function 
(#11285)
f336100ccd is described below

commit f336100ccdc7c7f558d83dbb9f8e89b2a3d620c5
Author: Zkplo <87751516+zk...@users.noreply.github.com>
AuthorDate: Tue Oct 8 20:16:16 2024 +0800

    [INLONG-11263][SDK] Transform SQL supports "lcm" function (#11285)
---
 .../transform/process/function/LcmFunction.java    | 95 ++++++++++++++++++++++
 .../function/arithmetic/TestLcmFunction.java       | 70 ++++++++++++++++
 2 files changed, 165 insertions(+)

diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LcmFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LcmFunction.java
new file mode 100644
index 0000000000..8610b7bcf8
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LcmFunction.java
@@ -0,0 +1,95 @@
+/*
+ * 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 lombok.extern.slf4j.Slf4j;
+import net.sf.jsqlparser.expression.Expression;
+import net.sf.jsqlparser.expression.Function;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.math.RoundingMode;
+import java.util.List;
+
+/**
+ * LcmFunction -> lcm(numeric_type,numeric_type)
+ * description:
+ * - return 0 if either input is zero
+ * - return least common multiple (the smallest strictly positive number that 
is an integral multiple of both inputs)
+ * Note: numeric_type includes floating-point number and integer
+ */
+@Slf4j
+@TransformFunction(names = {"lcm"})
+public class LcmFunction implements ValueParser {
+
+    private final ValueParser firstNumParser;
+    private final ValueParser secondNumTypeParser;
+
+    public LcmFunction(Function expr) {
+        List<Expression> expressions = expr.getParameters().getExpressions();
+        firstNumParser = OperatorTools.buildParser(expressions.get(0));
+        secondNumTypeParser = OperatorTools.buildParser(expressions.get(1));
+    }
+
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        Object firstNumObj = firstNumParser.parse(sourceData, rowIndex, 
context);
+        Object secondNumObj = secondNumTypeParser.parse(sourceData, rowIndex, 
context);
+        if (firstNumObj == null || secondNumObj == null) {
+            return null;
+        }
+        try {
+            BigDecimal firstNum = OperatorTools.parseBigDecimal(firstNumObj);
+            BigDecimal secondNum = OperatorTools.parseBigDecimal(secondNumObj);
+            return lcm(firstNum, secondNum).toPlainString();
+        } catch (Exception e) {
+            log.error("Parse error", e);
+            return null;
+        }
+    }
+
+    public static BigInteger gcd(BigDecimal a, BigDecimal b) {
+        BigInteger intA = a.toBigInteger();
+        BigInteger intB = b.toBigInteger();
+        return intA.gcd(intB);
+    }
+
+    public static BigDecimal lcm(BigDecimal a, BigDecimal b) {
+        if (a.compareTo(BigDecimal.ZERO) == 0 || b.compareTo(BigDecimal.ZERO) 
== 0) {
+            return BigDecimal.ZERO;
+        }
+        int maxScale = Math.max(a.scale(), b.scale());
+
+        BigDecimal factorA = a.movePointRight(maxScale);
+        BigDecimal factorB = b.movePointRight(maxScale);
+
+        BigInteger gcdValue = gcd(factorA, factorB);
+        BigDecimal product = factorA.multiply(factorB);
+
+        // LCM = (|a * b| / GCD(a, b))
+        BigDecimal lcmValue = product.divide(new BigDecimal(gcdValue), 
RoundingMode.HALF_UP);
+
+        return lcmValue.movePointLeft(maxScale).stripTrailingZeros();
+    }
+
+}
\ No newline at end of file
diff --git 
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLcmFunction.java
 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLcmFunction.java
new file mode 100644
index 0000000000..3550912fbd
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLcmFunction.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.inlong.sdk.transform.process.function.arithmetic;
+
+import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory;
+import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory;
+import org.apache.inlong.sdk.transform.pojo.TransformConfig;
+import org.apache.inlong.sdk.transform.process.TransformProcessor;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.List;
+
+public class TestLcmFunction extends AbstractFunctionArithmeticTestBase {
+
+    @Test
+    public void testLcmFunction() throws Exception {
+        String transformSql = null, data = null;
+        TransformConfig config = null;
+        TransformProcessor<String, String> processor = null;
+        List<String> output = null;
+
+        transformSql = "select lcm(numeric1,numeric2) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+
+        // case1: lcm(3.14159265358979323846,3.2653589793)
+        data = "3.14159265358979323846|3.2653589793||";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=512921389035117286501.7893551939", 
output.get(0));
+
+        // case2: lcm(3.141,3.846)
+        data = "3.141|3.846||";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=4026.762", output.get(0));
+
+        // case3: lcm(0,3.846)
+        data = "0|3.846||";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=0", output.get(0));
+
+        // case4: lcm(-3.141,3.846)
+        data = "3.141|3.846||";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=4026.762", output.get(0));
+    }
+}

Reply via email to