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 1394dcdbea [INLONG-10864][SDK] Transform SQL support md5 function 
(#10865)
1394dcdbea is described below

commit 1394dcdbea4a18b858d02b4f5319dabcd8dab4a3
Author: Zkplo <87751516+zk...@users.noreply.github.com>
AuthorDate: Wed Aug 28 15:09:23 2024 +0800

    [INLONG-10864][SDK] Transform SQL support md5 function (#10865)
---
 .../transform/process/function/Md5Function.java    | 51 ++++++++++++++++++++++
 .../transform/process/operator/OperatorTools.java  |  2 +
 .../TestTransformArithmeticFunctionsProcessor.java | 33 ++++++++++++++
 3 files changed, 86 insertions(+)

diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/Md5Function.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/Md5Function.java
new file mode 100644
index 0000000000..027511864a
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/Md5Function.java
@@ -0,0 +1,51 @@
+/*
+ * 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 org.apache.commons.codec.digest.DigestUtils;
+
+import java.nio.charset.StandardCharsets;
+
+/**
+ * Md5Function
+ * description: MD5(string): Return the MD5 hash value of a string in the form 
of a 32-bit hexadecimal digit string; If the string is NULL, return NULL.
+ */
+public class Md5Function implements ValueParser {
+
+    private ValueParser msgParser;
+
+    public Md5Function(Function expr) {
+        msgParser = 
OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
+    }
+
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        Object msgObj = msgParser.parse(sourceData, rowIndex, context);
+        if (msgObj == null) {
+            return null;
+        }
+        String msg = msgObj.toString();
+        return DigestUtils.md5Hex(msg.getBytes(StandardCharsets.UTF_8));
+    }
+}
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 66c59a888b..fbd52185e0 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.Log10Function;
 import org.apache.inlong.sdk.transform.process.function.Log2Function;
 import org.apache.inlong.sdk.transform.process.function.LogFunction;
 import org.apache.inlong.sdk.transform.process.function.LowerFunction;
+import org.apache.inlong.sdk.transform.process.function.Md5Function;
 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;
@@ -177,6 +178,7 @@ public class OperatorTools {
         functionMap.put("left", LeftFunction::new);
         functionMap.put("right", RightFunction::new);
         functionMap.put("timestampadd", TimestampAddFunction::new);
+        functionMap.put("md5", Md5Function::new);
     }
 
     public static ExpressionOperator buildOperator(Expression expr) {
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 b1c2232f15..1467636e65 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
@@ -224,6 +224,39 @@ public class TestTransformArithmeticFunctionsProcessor {
 
     }
 
+    @Test
+    public void testMd5Function() throws Exception {
+        String transformSql = "select md5(numeric1) from source";
+        TransformConfig config = new TransformConfig(transformSql);
+        TransformProcessor<String, String> processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        // case1: md5("1")
+        List<String> output1 = processor.transform("1|4|6|8");
+        Assert.assertEquals(1, output1.size());
+        Assert.assertEquals("result=c4ca4238a0b923820dcc509a6f75849b", 
output1.get(0));
+
+        // case2: md5("-1")
+        List<String> output2 = processor.transform("-1|4|6|8");
+        Assert.assertEquals(1, output2.size());
+        Assert.assertEquals("result=6bb61e3b7bce0931da574d19d1d82c88", 
output2.get(0));
+
+        // case3: md5("")
+        List<String> output3 = processor.transform("|4|6|8");
+        Assert.assertEquals(1, output3.size());
+        Assert.assertEquals("result=d41d8cd98f00b204e9800998ecf8427e", 
output3.get(0));
+
+        // case4: md5(null)
+        transformSql = "select md5(numericxx) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        List<String> output4 = processor.transform("1|4|6|8");
+        Assert.assertEquals(1, output4.size());
+        Assert.assertEquals("result=null", output4.get(0));
+    }
+
     @Test
     public void testRoundFunction() throws Exception {
         String transformSql = "select round(numeric1) from source";

Reply via email to