This is an automated email from the ASF dual-hosted git repository.

dockerzhang 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 a34d877a3d [INLONG-10927][SDK] Transform supports padding of left and 
right strings (#10928)
a34d877a3d is described below

commit a34d877a3d9e69443f9497a27b4cf3b13a7e9b78
Author: Zkplo <87751516+zk...@users.noreply.github.com>
AuthorDate: Mon Sep 2 09:42:41 2024 +0800

    [INLONG-10927][SDK] Transform supports padding of left and right strings 
(#10928)
    
    Co-authored-by: ZKpLo <14148880+zk...@user.noreply.gitee.com>
---
 .../transform/process/function/LpadFunction.java   |  81 ++++++++++++
 .../transform/process/function/RpadFunction.java   |  81 ++++++++++++
 .../transform/process/operator/OperatorTools.java  |   2 +-
 .../TestTransformStringFunctionsProcessor.java     | 140 +++++++++++++++++++++
 4 files changed, 303 insertions(+), 1 deletion(-)

diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LpadFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LpadFunction.java
new file mode 100644
index 0000000000..cce51ba1e4
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LpadFunction.java
@@ -0,0 +1,81 @@
+/*
+ * 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.util.List;
+
+/**
+ * LpadFunction
+ * description: Lpad(s1,len,s2) Fill string s2 at the beginning of string s1 
to make the string length len
+ * - return null if any of the three parameters is null or len is less than 0
+ * - return the substring of s1 with subscripts in the range of [0, len) if 
len is less than or equal to the length of s1
+ * - if s2 is ""
+ *      - return "" if len is longer than the length of s1
+ * - if s2 is not ""
+ *      - return the filled string
+ */
+@TransformFunction(names = {"lpad"})
+public class LpadFunction implements ValueParser {
+
+    private final ValueParser leftStringParser;
+    private final ValueParser lengthParser;
+    private final ValueParser rightStringParser;
+
+    public LpadFunction(Function expr) {
+        List<Expression> expressions = expr.getParameters().getExpressions();
+        leftStringParser = OperatorTools.buildParser(expressions.get(0));
+        lengthParser = OperatorTools.buildParser(expressions.get(1));
+        rightStringParser = OperatorTools.buildParser(expressions.get(2));
+    }
+
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        Object leftStringObj = leftStringParser.parse(sourceData, rowIndex, 
context);
+        Object lengthObj = lengthParser.parse(sourceData, rowIndex, context);
+        Object rightStringObj = rightStringParser.parse(sourceData, rowIndex, 
context);
+        if (leftStringObj == null || lengthObj == null || rightStringObj == 
null) {
+            return null;
+        }
+        int len = Integer.parseInt(OperatorTools.parseString(lengthObj));
+        if (len < 0) {
+            return null;
+        }
+        String leftStr = OperatorTools.parseString(leftStringObj);
+        if (len <= leftStr.length()) {
+            return leftStr.substring(0, len);
+        }
+        String rightStr = OperatorTools.parseString(rightStringObj);
+        if (rightStr.isEmpty()) {
+            return "";
+        }
+        int padLen = len - leftStr.length();
+        StringBuilder builder = new StringBuilder(padLen);
+        while (builder.length() < padLen) {
+            builder.append(rightStr);
+        }
+        return builder.substring(0, padLen).concat(leftStr);
+    }
+}
diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RpadFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RpadFunction.java
new file mode 100644
index 0000000000..c3357c7b55
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RpadFunction.java
@@ -0,0 +1,81 @@
+/*
+ * 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.util.List;
+
+/**
+ * RpadFunction
+ * description: RPAD(s1,len,s2) : Fill string s2 at the end of string s1 to 
make the length of the string len
+ * - return null if any of the three parameters is null or len is less than 0
+ * - return the substring of s1 with subscripts in the range of [0, len) if 
len is less than or equal to the length of s1
+ * - if s2 is ""
+ *      - return "" if len is longer than the length of s1
+ * - if s2 is not ""
+ *      - return the filled string
+ */
+@TransformFunction(names = {"rpad"})
+public class RpadFunction implements ValueParser {
+
+    private final ValueParser leftStringParser;
+    private final ValueParser lengthParser;
+    private final ValueParser rightStringParser;
+
+    public RpadFunction(Function expr) {
+        List<Expression> expressions = expr.getParameters().getExpressions();
+        leftStringParser = OperatorTools.buildParser(expressions.get(0));
+        lengthParser = OperatorTools.buildParser(expressions.get(1));
+        rightStringParser = OperatorTools.buildParser(expressions.get(2));
+    }
+
+    @Override
+    public Object parse(SourceData sourceData, int rowIndex, Context context) {
+        Object leftStringObj = leftStringParser.parse(sourceData, rowIndex, 
context);
+        Object lengthObj = lengthParser.parse(sourceData, rowIndex, context);
+        Object rightStringObj = rightStringParser.parse(sourceData, rowIndex, 
context);
+        if (leftStringObj == null || lengthObj == null || rightStringObj == 
null) {
+            return null;
+        }
+        int len = Integer.parseInt(OperatorTools.parseString(lengthObj));
+        if (len < 0) {
+            return null;
+        }
+        String leftStr = OperatorTools.parseString(leftStringObj);
+        if (len <= leftStr.length()) {
+            return leftStr.substring(0, len);
+        }
+        String rightStr = OperatorTools.parseString(rightStringObj);
+        if (rightStr.isEmpty()) {
+            return "";
+        }
+        StringBuilder builder = new StringBuilder(len);
+        builder.append(leftStr);
+        while (builder.length() < len) {
+            builder.append(rightStr);
+        }
+        return builder.substring(0, len);
+    }
+}
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 dbe12ff814..5e69984564 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
@@ -42,7 +42,7 @@ import java.sql.Timestamp;
 
 /**
  * OperatorTools
- * 
+ *
  */
 public class OperatorTools {
 
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 10cfa740c9..1451194607 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
@@ -428,6 +428,146 @@ public class TestTransformStringFunctionsProcessor {
         Assert.assertEquals("result=null", output.get(0));
     }
 
+    @Test
+    public void testRpadFunction() throws Exception {
+        String transformSql = null, data = null;
+        TransformConfig config = null;
+        TransformProcessor<String, String> processor = null;
+        List<String> output = null;
+
+        transformSql = "select rpad(string1,numeric1,string2) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        // case1: rpad('he',7,'xxd')
+        data = "he|xxd|cloud|7|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=hexxdxx", output.get(0));
+
+        // case2: rpad('he',1,'xxd')
+        data = "he|xxd|cloud|1|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=h", output.get(0));
+
+        // case3: rpad('he',1,'')
+        data = "he||cloud|1|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=h", output.get(0));
+
+        // case4: rpad('he',-1,'xxd')
+        data = "he|xxd|cloud|-1|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=null", output.get(0));
+
+        // case5: rpad(null,5,'xxd')
+        transformSql = "select rpad(xxd,numeric1,string2) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        data = "he|xxd|cloud|5|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=null", output.get(0));
+
+        // case6: rpad('he',null,'xxd')
+        transformSql = "select rpad(string1,xxd,string2) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        data = "he|xxd|cloud|5|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=null", output.get(0));
+
+        // case7: rpad('he',5,null)
+        transformSql = "select rpad(string1,numeric1,xxd) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        data = "he|xxd|cloud|5|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=null", output.get(0));
+    }
+
+    @Test
+    public void testLpadFunction() throws Exception {
+        String transformSql = null, data = null;
+        TransformConfig config = null;
+        TransformProcessor<String, String> processor = null;
+        List<String> output = null;
+
+        transformSql = "select lpad(string1,numeric1,string2) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        // case1: lpad('he',7,'xxd')
+        data = "he|xxd|cloud|7|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=xxdxxhe", output.get(0));
+
+        // case2: lpad('he',1,'xxd')
+        data = "he|xxd|cloud|1|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=h", output.get(0));
+
+        // case3: lpad('he',1,'')
+        data = "he||cloud|1|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=h", output.get(0));
+
+        // case4: lpad('he',-1,'xxd')
+        data = "he|xxd|cloud|-1|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=null", output.get(0));
+
+        // case5: lpad(null,5,'xxd')
+        transformSql = "select lpad(xxd,numeric1,string2) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        data = "he|xxd|cloud|5|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=null", output.get(0));
+
+        // case6: lpad('he',null,'xxd')
+        transformSql = "select lpad(string1,xxd,string2) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        data = "he|xxd|cloud|5|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=null", output.get(0));
+
+        // case7: lpad('he',5,null)
+        transformSql = "select lpad(string1,numeric1,xxd) from source";
+        config = new TransformConfig(transformSql);
+        processor = TransformProcessor
+                .create(config, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        data = "he|xxd|cloud|5|3|3";
+        output = processor.transform(data, new HashMap<>());
+        Assert.assertEquals(1, output.size());
+        Assert.assertEquals("result=null", output.get(0));
+    }
+
     @Test
     public void testRightFunction() throws Exception {
         String transformSql = "select right(string1,numeric1) from source";

Reply via email to