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 d5a4ebadce [INLONG-10876][SDK] Transform support RAND() function 
(#10877)
d5a4ebadce is described below

commit d5a4ebadcef064993164b11ee26bc27b1e98a95b
Author: emptyOVO <118812562+empty...@users.noreply.github.com>
AuthorDate: Tue Aug 27 19:34:43 2024 +0800

    [INLONG-10876][SDK] Transform support RAND() function (#10877)
    
    * fix: conflicts
    
    * fix: prepare Random Object in the constructor, check expression list size 
at first, avoid to use NullPointerException
    
    * fix: check expressions.size()==1
    
    * fix: conficts
    
    * fix: conficts
---
 .../transform/process/function/RandFunction.java   | 61 ++++++++++++++++++++++
 .../transform/process/operator/OperatorTools.java  |  2 +
 .../TestTransformArithmeticFunctionsProcessor.java | 28 ++++++++++
 3 files changed, 91 insertions(+)

diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RandFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RandFunction.java
new file mode 100644
index 0000000000..2baebe038a
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RandFunction.java
@@ -0,0 +1,61 @@
+/*
+ * 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.Expression;
+import net.sf.jsqlparser.expression.Function;
+
+import java.math.BigDecimal;
+import java.util.List;
+import java.util.Random;
+
+/**
+ * RandFunction
+ * description: Rand()--Returns a pseudo-random double precision value in the 
range [0.0, 1.0)
+ *              Rand(Integer)--Returns a pseudo-random double precision value 
in the range [0.0, 1.0) with an initial seed of Integer.
+ */
+public class RandFunction implements ValueParser {
+
+    private ValueParser seedParser;
+
+    private Random random;
+
+    public RandFunction(Function expr) {
+        random = new Random();
+        if (expr.getParameters() != null) {
+            List<Expression> expressions = 
expr.getParameters().getExpressions();
+            if (expressions != null && expressions.size() == 1) {
+                seedParser = OperatorTools.buildParser(expressions.get(0));
+            }
+        }
+    }
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        if (seedParser != null) {
+            Object seedObj = seedParser.parse(sourceData, rowIndex, context);
+            BigDecimal seedValue = OperatorTools.parseBigDecimal(seedObj);
+            random.setSeed(seedValue.intValue());
+        }
+        return random.nextDouble();
+    }
+}
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 1cb1b8589e..ed7ba57d4b 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
@@ -38,6 +38,7 @@ import 
org.apache.inlong.sdk.transform.process.function.LogFunction;
 import org.apache.inlong.sdk.transform.process.function.ModuloFunction;
 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.RandFunction;
 import org.apache.inlong.sdk.transform.process.function.ReplaceFunction;
 import org.apache.inlong.sdk.transform.process.function.ReplicateFunction;
 import org.apache.inlong.sdk.transform.process.function.ReverseFunction;
@@ -131,6 +132,7 @@ public class OperatorTools {
         functionMap.put("to_date", ToDateFunction::new);
         functionMap.put("date_format", DateFormatFunction::new);
         functionMap.put("ceil", CeilFunction::new);
+        functionMap.put("rand", RandFunction::new);
         functionMap.put("floor", FloorFunction::new);
         functionMap.put("sin", SinFunction::new);
         functionMap.put("sinh", SinhFunction::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 99f45bc693..08a2430e2d 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
@@ -30,6 +30,7 @@ import org.junit.Test;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Random;
 
 /**
  * TestArithmeticFunctionsTransformProcessor
@@ -537,4 +538,31 @@ public class TestTransformArithmeticFunctionsProcessor {
         Assert.assertEquals(output2.get(0), "result=null");
     }
 
+    @Test
+    public void testRandFunction() throws Exception {
+        String transformSql1 = "select rand(numeric1) from source";
+        TransformConfig config1 = new TransformConfig(transformSql1);
+        TransformProcessor<String, String> processor1 = TransformProcessor
+                .create(config1, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        // case: rand(1)
+        List<String> output1 = processor1.transform("1|4|6|8", new 
HashMap<>());
+        Assert.assertEquals(1, output1.size());
+        Assert.assertEquals(output1.get(0), "result=" + new 
Random(1).nextDouble());
+        // case: rand(2)
+        List<String> output2 = processor1.transform("2|4|6|8", new 
HashMap<>());
+        Assert.assertEquals(1, output2.size());
+        Assert.assertEquals(output2.get(0), "result=" + new 
Random(2).nextDouble());
+        String transformSql2 = "select rand() from source";
+        TransformConfig config2 = new TransformConfig(transformSql2);
+        TransformProcessor<String, String> processor2 = TransformProcessor
+                .create(config2, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        // case: rand()
+        List<String> output3 = processor2.transform("|||", new HashMap<>());
+        Assert.assertEquals(1, output3.size());
+        double result = Double.parseDouble(output3.get(0).substring(7));
+        Assert.assertTrue(result >= 0.0 && result < 1.0);
+    }
+
 }

Reply via email to