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 20, 2024
1 parent a46d4da commit f16215d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 stringParser1;
private ValueParser stringParser2;
private ValueParser stringParser3;

/**
* Constructor
* @param expr
*/
public ReplaceFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
stringParser1 = OperatorTools.buildParser(expressions.get(0));
stringParser2 = OperatorTools.buildParser(expressions.get(1));
stringParser3 = OperatorTools.buildParser(expressions.get(2));
}

@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object stringObj1 = stringParser1.parse(sourceData, rowIndex, context);
Object stringObj2 = stringParser2.parse(sourceData, rowIndex, context);
Object stringObj3 = stringParser3.parse(sourceData, rowIndex, context);
String str1 = OperatorTools.parseString(stringObj1);
String str2 = OperatorTools.parseString(stringObj2);
String str3 = OperatorTools.parseString(stringObj3);
return str1.replace(str2.charAt(0), str3.charAt(0));
}
}
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.RoundFunction;
import org.apache.inlong.sdk.transform.process.function.SinFunction;
import org.apache.inlong.sdk.transform.process.function.SinhFunction;
Expand Down Expand Up @@ -108,6 +109,7 @@ public class OperatorTools {
functionMap.put("log", LogFunction::new);
functionMap.put("exp", ExpFunction::new);
functionMap.put("substring", SubstringFunction::new);
functionMap.put("replace", ReplaceFunction::new);
functionMap.put("locate", LocateFunction::new);
functionMap.put("to_date", ToDateFunction::new);
functionMap.put("date_format", DateFormatFunction::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,19 @@ public void testLocateFunction() throws Exception {
Assert.assertEquals(1, output5.size());
Assert.assertEquals(output5.get(0), "result=null");
}

@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', 'o', 'a')
List<String> output = processor.transform("hooray|o|a", new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals(output.get(0), "result=haaray");

}

}

0 comments on commit f16215d

Please sign in to comment.