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 cd2a0a246d [INLONG-10873][SDK] Transform SQL support factorial 
function (#11623)
cd2a0a246d is described below

commit cd2a0a246dec24e1d773515d0f6fee4f6910a777
Author: emptyOVO <118812562+empty...@users.noreply.github.com>
AuthorDate: Mon Dec 30 20:17:48 2024 +0800

    [INLONG-10873][SDK] Transform SQL support factorial function (#11623)
---
 .../function/arithmetic/FactorialFunction.java     | 90 ++++++++++++++++++++++
 .../function/arithmetic/TestFactorialFunction.java | 62 +++++++++++++++
 2 files changed, 152 insertions(+)

diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/arithmetic/FactorialFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/arithmetic/FactorialFunction.java
new file mode 100644
index 0000000000..7ac9e86401
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/arithmetic/FactorialFunction.java
@@ -0,0 +1,90 @@
+/*
+ * 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.SourceData;
+import org.apache.inlong.sdk.transform.process.Context;
+import org.apache.inlong.sdk.transform.process.function.FunctionConstant;
+import org.apache.inlong.sdk.transform.process.function.TransformFunction;
+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;
+
+/**
+ * FactorialFunction  ->  factorial(numeric)
+ * Description:
+ * - Return NULL if 'numeric' is NULL;
+ * - Return the factorial of a non-negative
+ */
+@TransformFunction(type = FunctionConstant.ARITHMETIC_TYPE, names = {
+        "factorial"}, parameter = "(Numeric numeric)", descriptions = {
+                "- Return \"\" if 'numeric' is NULL;",
+                "- Return the factorial of a non-negative."
+        }, examples = {
+                "factorial(5) = 120",
+                "factorial(0) = 1"
+        })
+@Slf4j
+public class FactorialFunction implements ValueParser {
+
+    private ValueParser numberParser;
+
+    public FactorialFunction(Function expr) {
+        if (expr.getParameters() != null) {
+            List<Expression> expressions = 
expr.getParameters().getExpressions();
+            if (expressions != null && expressions.size() == 1) {
+                numberParser = OperatorTools.buildParser(expressions.get(0));
+            }
+        }
+    }
+
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        if (numberParser != null) {
+            Object valueObj = numberParser.parse(sourceData, rowIndex, 
context);
+            if (valueObj == null) {
+                return null;
+            }
+            BigDecimal value = OperatorTools.parseBigDecimal(valueObj);
+            if (value.scale() > 0 || value.compareTo(BigDecimal.ZERO) < 0) {
+                log.warn("Factorial is only defined for non-negative integers. 
Invalid input: {}", value);
+                return null;
+            }
+            return factorial(value.intValue());
+        }
+        return null;
+    }
+
+    private BigDecimal factorial(int n) {
+        if (n < 0) {
+            log.error("Factorial is not defined for negative numbers.");
+            return null;
+        }
+        BigDecimal result = BigDecimal.ONE;
+        for (int i = 2; i <= n; i++) {
+            result = result.multiply(BigDecimal.valueOf(i));
+        }
+        return result;
+    }
+}
diff --git 
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestFactorialFunction.java
 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestFactorialFunction.java
new file mode 100644
index 0000000000..b4b52af876
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestFactorialFunction.java
@@ -0,0 +1,62 @@
+/*
+ * 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.List;
+
+public class TestFactorialFunction extends AbstractFunctionArithmeticTestBase {
+
+    @Test
+    public void testFactorialFunction() throws Exception {
+
+        String transformSql = "select factorial(numeric1) from source";
+        TransformConfig config = new TransformConfig(transformSql);
+        TransformProcessor<String, String> processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+
+        // case1: Valid input - 5!
+        List<String> output1 = processor.transform("5|4|6|8");
+        Assert.assertEquals(1, output1.size());
+        Assert.assertEquals("result=120", output1.get(0));
+
+        // case2: Valid input - 0!
+        List<String> output2 = processor.transform("0|4|6|8");
+        Assert.assertEquals(1, output2.size());
+        Assert.assertEquals("result=1", output2.get(0));
+
+        // case3: Non-integer input (5.5) should return null
+        List<String> output3 = processor.transform("5.5|4|6|8");
+        Assert.assertEquals(1, output3.size());
+        Assert.assertEquals("result=", output3.get(0));
+
+        // case4: Negative input (-5) should return null
+        List<String> output4 = processor.transform("-5|4|6|8");
+        Assert.assertEquals(1, output4.size());
+        Assert.assertEquals("result=", output4.get(0));
+    }
+
+}

Reply via email to