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 04e3a58ea1 [INLONG-10816][SDK] Transform support Replace
function.(#10816) (#10818)
04e3a58ea1 is described below
commit 04e3a58ea17c166d6d6394a68a9707c3649bef73
Author: rachely <[email protected]>
AuthorDate: Tue Aug 27 14:16:50 2024 +0800
[INLONG-10816][SDK] Transform support Replace function.(#10816) (#10818)
---
.../process/function/ReplaceFunction.java | 57 ++++++++++++++++++++++
.../transform/process/operator/OperatorTools.java | 2 +
.../TestTransformStringFunctionsProcessor.java | 36 ++++++++++++++
3 files changed, 95 insertions(+)
diff --git
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReplaceFunction.java
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReplaceFunction.java
new file mode 100644
index 0000000000..d9d1d26a21
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReplaceFunction.java
@@ -0,0 +1,57 @@
+/*
+ * 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;
+
+/**
+ * ReplaceFunction
+ * description: replace(s, s1, s2)--replace string s1 in string s with string
s2.
+ */
+public class ReplaceFunction implements ValueParser {
+
+ private ValueParser stringParser;
+ private ValueParser targetParser;
+ private ValueParser replacementParser;
+
+ public ReplaceFunction(Function expr) {
+ List<Expression> expressions = expr.getParameters().getExpressions();
+ stringParser = OperatorTools.buildParser(expressions.get(0));
+ targetParser = OperatorTools.buildParser(expressions.get(1));
+ replacementParser = OperatorTools.buildParser(expressions.get(2));
+ }
+
+ @Override
+ public Object parse(SourceData sourceData, int rowIndex, Context context) {
+ Object strObj = stringParser.parse(sourceData, rowIndex, context);
+ Object targetObj = targetParser.parse(sourceData, rowIndex, context);
+ Object replacementObj = replacementParser.parse(sourceData, rowIndex,
context);
+ String str = OperatorTools.parseString(strObj);
+ String target = OperatorTools.parseString(targetObj);
+ String replacement = OperatorTools.parseString(replacementObj);
+ return str.replace(target, replacement);
+ }
+}
\ No newline at end of file
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 9c05508db0..f7f023e748 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
@@ -37,6 +37,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.ReplaceFunction;
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;
@@ -156,6 +157,7 @@ public class OperatorTools {
functionMap.put("mod", ModuloFunction::new);
functionMap.put("to_base64", ToBase64Function::new);
functionMap.put("length", LengthFunction::new);
+ functionMap.put("replace", ReplaceFunction::new);
}
public static ExpressionOperator buildOperator(Expression expr) {
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 a8a3ae68ec..b489a89218 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
@@ -255,4 +255,40 @@ public class TestTransformStringFunctionsProcessor {
Assert.assertEquals(1, output1.size());
Assert.assertEquals("result=null", output1.get(0));
}
+ @Test
+ public void testReplaceFunction() throws Exception {
+ String transformSql = "select replace(string1, string2, string3) from
source";
+ TransformConfig config = new TransformConfig(transformSql);
+ TransformProcessor<String, String> processor = TransformProcessor
+ .create(config,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+ // case1: replace('hooray', 'oray', 'lly')
+ List<String> output1 = processor.transform("hooray|oray|lly", new
HashMap<>());
+ Assert.assertEquals(1, output1.size());
+ Assert.assertEquals(output1.get(0), "result=holly");
+ // case2: replace('hooray', 'hook', 'hoor')
+ List<String> output2 = processor.transform("hooray|hook|hoor", new
HashMap<>());
+ Assert.assertEquals(1, output2.size());
+ Assert.assertEquals(output2.get(0), "result=hooray");
+ // case3: replace('Hello World', 'World', '')
+ List<String> output3 = processor.transform("Hello World|World|", new
HashMap<>());
+ Assert.assertEquals(1, output3.size());
+ Assert.assertEquals(output3.get(0), "result=Hello ");
+ // case4: replace('Hello World', '', 'J')
+ List<String> output4 = processor.transform("Hello World||J", new
HashMap<>());
+ Assert.assertEquals(1, output4.size());
+ Assert.assertEquals(output4.get(0), "result=JHJeJlJlJoJ JWJoJrJlJdJ");
+ // case5: replace('', '', '')
+ List<String> output5 = processor.transform("||", new HashMap<>());
+ Assert.assertEquals(1, output5.size());
+ Assert.assertEquals(output5.get(0), "result=");
+ // case6: replace('abababab', 'ab', 'cd')
+ List<String> output6 = processor.transform("abababab|ab|cd", new
HashMap<>());
+ Assert.assertEquals(1, output6.size());
+ Assert.assertEquals(output6.get(0), "result=cdcdcdcd");
+ // case7: replace('aaa', 'aa', 'd')
+ List<String> output7 = processor.transform("aaa|aa|d", new
HashMap<>());
+ Assert.assertEquals(1, output7.size());
+ Assert.assertEquals(output7.get(0), "result=da");
+ }
}