Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INLONG-10816][SDK] Transform support Replace function. #10818

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -37,6 +37,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.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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,40 @@ public void testLengthFunction() throws Exception {
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");
}
}
Loading