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 6ce335e872 [INLONG-10823][SDK] Transform SQL support Reverse function 
(#10854)
6ce335e872 is described below

commit 6ce335e872d806847fa42c33f8e3b16e0a3d2b52
Author: Xincheng Huang <60057611+ying-...@users.noreply.github.com>
AuthorDate: Tue Aug 27 12:08:17 2024 +0800

    [INLONG-10823][SDK] Transform SQL support Reverse function (#10854)
---
 .../process/function/ReverseFunction.java          | 56 ++++++++++++++++++++++
 .../transform/process/operator/OperatorTools.java  |  2 +
 .../TestTransformStringFunctionsProcessor.java     | 32 +++++++++++++
 3 files changed, 90 insertions(+)

diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReverseFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReverseFunction.java
new file mode 100644
index 0000000000..5015e0225a
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReverseFunction.java
@@ -0,0 +1,56 @@
+/*
+ * 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;
+/**
+ * ReverseFunction
+ * description: reverse(string)--returns the string with the order of the 
characters reversed.
+ * returns NULL if string is a empty string.
+ */
+public class ReverseFunction implements ValueParser {
+
+    private ValueParser stringParser;
+    public ReverseFunction(Function expr) {
+        stringParser = 
OperatorTools.buildParser(expr.getParameters().getExpressions().get(0));
+    }
+
+    /**
+     * parse
+     * Parse and reverse string by reverse() in StringBuilder.
+     * @param sourceData
+     * @param rowIndex
+     * @param context
+     * @return
+     */
+
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        Object stringObj = stringParser.parse(sourceData, rowIndex, context);
+        if (stringObj == null) {
+            return null;
+        }
+        String str = OperatorTools.parseString(stringObj);
+        return new StringBuilder(str).reverse().toString();
+    }
+}
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 2305d5165c..9c05508db0 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.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.ReplicateFunction;
+import org.apache.inlong.sdk.transform.process.function.ReverseFunction;
 import org.apache.inlong.sdk.transform.process.function.RoundFunction;
 import org.apache.inlong.sdk.transform.process.function.SinFunction;
 import org.apache.inlong.sdk.transform.process.function.SinhFunction;
@@ -120,6 +121,7 @@ public class OperatorTools {
         functionMap.put("log2", Log2Function::new);
         functionMap.put("log", LogFunction::new);
         functionMap.put("exp", ExpFunction::new);
+        functionMap.put("reverse", ReverseFunction::new);
         functionMap.put("substring", SubstringFunction::new);
         functionMap.put("trim", TrimFunction::new);
         functionMap.put("replicate", ReplicateFunction::new);
diff --git 
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java
 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java
index 96cabc1717..a8a3ae68ec 100644
--- 
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java
+++ 
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java
@@ -41,6 +41,7 @@ public class TestTransformStringFunctionsProcessor {
     private static final List<FieldInfo> dstFields = new ArrayList<>();
     private static final CsvSourceInfo csvSource;
     private static final KvSinkInfo kvSink;
+
     static {
         for (int i = 1; i < 4; i++) {
             FieldInfo field = new FieldInfo();
@@ -118,6 +119,7 @@ public class TestTransformStringFunctionsProcessor {
         Assert.assertEquals(1, output5.size());
         Assert.assertEquals(output5.get(0), "result=null");
     }
+
     @Test
     public void testReplicateFunction() throws Exception {
         String transformSql1 = "select replicate(string1, numeric1) from 
source";
@@ -184,6 +186,36 @@ public class TestTransformStringFunctionsProcessor {
         Assert.assertEquals(output3.get(0), "result=in long");
     }
 
+    @Test
+    public void testReverseFunction() throws Exception {
+        String transformSql1 = "select reverse(string1) from source";
+        TransformConfig config1 = new TransformConfig(transformSql1);
+        TransformProcessor<String, String> processor1 = TransformProcessor
+                .create(config1, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        // case1: reverse('apple')
+        List<String> output1 = 
processor1.transform("apple|banana|cloud|2|1|3", new HashMap<>());
+        Assert.assertEquals(1, output1.size());
+        Assert.assertEquals(output1.get(0), "result=elppa");
+        // case2: reverse('ban ana ')
+        String transformSql2 = "select reverse(string2) from source";
+        TransformConfig config2 = new TransformConfig(transformSql2);
+        TransformProcessor<String, String> processor2 = TransformProcessor
+                .create(config2, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        List<String> output2 = processor2.transform("apple|ban ana 
|cloud|2|1|3", new HashMap<>());
+        Assert.assertEquals(1, output2.size());
+        Assert.assertEquals(output2.get(0), "result= ana nab");
+        // case3: reverse(12345)
+        List<String> output3 = 
processor1.transform("12345|banana|cloud|2|1|3", new HashMap<>());
+        Assert.assertEquals(1, output3.size());
+        Assert.assertEquals(output3.get(0), "result=54321");
+        // case4: reverse(null)
+        List<String> output4 = processor1.transform("|banana|cloud|2|1|3", new 
HashMap<>());
+        Assert.assertEquals(1, output4.size());
+        Assert.assertEquals(output4.get(0), "result=");
+    }
+
     @Test
     public void testToBase64Function() throws Exception {
         String transformSql = "select to_base64(string1) from source";

Reply via email to