This is an automated email from the ASF dual-hosted git repository.
luchunliang 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 cde50b2bcb [INLONG-11121][SDK] Transform SQL supports gcd functions
(#11128)
cde50b2bcb is described below
commit cde50b2bcbed5b51f250d5725f46f082f56d5e93
Author: Zkplo <[email protected]>
AuthorDate: Thu Sep 19 11:15:36 2024 +0800
[INLONG-11121][SDK] Transform SQL supports gcd functions (#11128)
Co-authored-by: ZKpLo <[email protected]>
---
.../transform/process/function/GcdFunction.java | 95 ++++++++++++++++++++++
.../function/arithmetic/TestGcdFunction.java | 70 ++++++++++++++++
2 files changed, 165 insertions(+)
diff --git
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/GcdFunction.java
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/GcdFunction.java
new file mode 100644
index 0000000000..54de700d12
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/GcdFunction.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.util.List;
+
+/**
+ * GcdFunction -> gcd(numeric_type,numeric_type)
+ * description:
+ * - return 0 if both inputs are zero;
+ * - return greatest common divisor (the largest positive number that divides
both inputs with no remainder).
+ * Note: numeric_type includes floating-point number and integer
+ */
+@Slf4j
+@TransformFunction(names = {"gcd"})
+public class GcdFunction implements ValueParser {
+
+ private final ValueParser firstNumParser;
+ private final ValueParser secondNumTypeParser;
+
+ public GcdFunction(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 gcdForBigDecimals(firstNum, secondNum);
+ } catch (Exception e) {
+ log.error("Parse error", e);
+ return null;
+ }
+ }
+
+ public static BigDecimal gcd(BigDecimal a, BigDecimal b) {
+ if (b.compareTo(BigDecimal.ZERO) == 0) {
+ return a;
+ }
+ BigDecimal remainder = a.remainder(b);
+ return gcd(b, remainder);
+ }
+
+ /**
+ * Support floating-point and integer gcd
+ *
+ * @param a first number
+ * @param b second number
+ * @return The greatest common divisor of a and b
+ */
+ public static BigDecimal gcdForBigDecimals(BigDecimal a, BigDecimal b) {
+ int scaleA = a.scale();
+ int scaleB = b.scale();
+
+ BigDecimal scaledA = a.movePointRight(Math.max(scaleA, scaleB));
+ BigDecimal scaledB = b.movePointRight(Math.max(scaleA, scaleB));
+
+ BigDecimal gcdValue = gcd(scaledA, scaledB);
+
+ return gcdValue.movePointLeft(Math.max(scaleA, scaleB));
+ }
+}
\ No newline at end of file
diff --git
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestGcdFunction.java
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestGcdFunction.java
new file mode 100644
index 0000000000..f4d00c69ca
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestGcdFunction.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 TestGcdFunction extends AbstractFunctionArithmeticTestBase {
+
+ @Test
+ public void testGcdFunction() throws Exception {
+ String transformSql = null, data = null;
+ TransformConfig config = null;
+ TransformProcessor<String, String> processor = null;
+ List<String> output = null;
+
+ transformSql = "select gcd(numeric1,numeric2) from source";
+ config = new TransformConfig(transformSql);
+ processor = TransformProcessor
+ .create(config,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+
+ // case1: gcd(3.14159265358979323846,3.2653589793)
+ data = "3.14159265358979323846|3.2653589793||";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=2E-20", output.get(0));
+
+ // case2: gcd(3.141,3.846)
+ data = "3.141|3.846||";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=0.003", output.get(0));
+
+ // case3: gcd(0,3.846)
+ data = "0|3.846||";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=3.846", output.get(0));
+
+ // case4: gcd(-3.141,3.846)
+ data = "3.141|3.846||";
+ output = processor.transform(data, new HashMap<>());
+ Assert.assertEquals(1, output.size());
+ Assert.assertEquals("result=0.003", output.get(0));
+ }
+}