Skip to content

Commit

Permalink
[INLONG-10816][SDK] Transform support Replace function.(apache#10816)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ybszzzziz committed Aug 22, 2024
1 parent 2bb4c0e commit 17522d9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.inlong.sdk.transform.process.function.LogFunction;
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.RoundFunction;
import org.apache.inlong.sdk.transform.process.function.SinFunction;
Expand Down Expand Up @@ -142,6 +143,7 @@ public class OperatorTools {
functionMap.put("unix_timestamp", UnixTimestampFunction::new);
functionMap.put("to_timestamp", ToTimestampFunction::new);
functionMap.put("to_base64", ToBase64Function::new);
functionMap.put("replace", ReplaceFunction::new);
}

public static ExpressionOperator buildOperator(Expression expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,40 @@ public void testToBase64Function() throws Exception {
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=aGVsbG8gd29ybGQ=");
}
@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");
}
}

0 comments on commit 17522d9

Please sign in to comment.