Skip to content

Commit

Permalink
[INLONG-11036][SDK] Transform update ReplicateFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
emptyOVO committed Sep 7, 2024
1 parent 179e478 commit 1921b5e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@

import java.util.List;
/**
* ReplicateFunction
* description: replicate(string, numeric)--Repeat the string numeric times and return a new string
* RepeatFunction
* description: repeat(string, numeric)--Repeat the string numeric times and return a new string
* replicate(string, numeric)--Repeat the string numeric times and return a new string
*/
@TransformFunction(names = {"replicate"})
public class ReplicateFunction implements ValueParser {
@TransformFunction(names = {"repeat", "replicate"})
public class RepeatFunction implements ValueParser {

private ValueParser stringParser;

private ValueParser countParser;

public ReplicateFunction(Function expr) {
public RepeatFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
stringParser = OperatorTools.buildParser(expressions.get(0));
countParser = OperatorTools.buildParser(expressions.get(1));
Expand All @@ -47,6 +48,9 @@ public ReplicateFunction(Function expr) {
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object stringObj = stringParser.parse(sourceData, rowIndex, context);
Object countObj = countParser.parse(sourceData, rowIndex, context);
if(stringObj.equals("")||countObj.equals("")){
return "";
}
String str = OperatorTools.parseString(stringObj);
double count = OperatorTools.parseBigDecimal(countObj).doubleValue();
return repeat(str, count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,56 @@
import java.util.HashMap;
import java.util.List;

public class TestReplicateFunction extends AbstractFunctionStringTestBase {
public class TestRepeatFunction extends AbstractFunctionStringTestBase {

@Test
public void testReplicateFunction() throws Exception {
String transformSql1 = "select replicate(string1, numeric1) from source";
public void testRepeatFunction() throws Exception {
String transformSql1 = "select repeat(string1, numeric1) from source";
TransformConfig config1 = new TransformConfig(transformSql1);
TransformProcessor<String, String> processor1 = TransformProcessor
.create(config1, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case1: replicate('apple', 2)
// case1: repeat('apple', 2)
List<String> output1 = processor1.transform("apple|banana|cloud|2|1|3", new HashMap<>());
Assert.assertEquals(1, output1.size());
Assert.assertEquals(output1.get(0), "result=appleapple");
String transformSql2 = "select replicate(string2, numeric2) from source";
String transformSql2 = "select repeat(string2, numeric2) from source";
TransformConfig config2 = new TransformConfig(transformSql2);
TransformProcessor<String, String> processor2 = TransformProcessor
.create(config2, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case2: replicate('banana', 3)
// case2: repeat('banana', 3)
List<String> output2 = processor2.transform("apple|banana|cloud|1|3|3", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals(output2.get(0), "result=bananabananabanana");
// case3: replicate('banana', 1)
// case3: repeat('banana', 1)
List<String> output3 = processor2.transform("apple|banana|cloud|1|1|3", new HashMap<>());
Assert.assertEquals(1, output2.size());
Assert.assertEquals(1, output3.size());
Assert.assertEquals(output3.get(0), "result=banana");
// case3: replicate('cloud', 0)
// case4: repeat('', 1)
List<String> output4 = processor2.transform("apple||cloud|1|1|3", new HashMap<>());
Assert.assertEquals(1, output4.size());
Assert.assertEquals(output4.get(0), "result=");
// case5: repeat('banana',)
List<String> output5 = processor2.transform("apple|banana|cloud|1||3", new HashMap<>());
Assert.assertEquals(1, output5.size());
Assert.assertEquals(output5.get(0), "result=");
// case6: repeat('banana',-2)
List<String> output6 = processor2.transform("apple|banana|cloud|1|-2|3", new HashMap<>());
Assert.assertEquals(1, output6.size());
Assert.assertEquals(output6.get(0), "result=");
String transformSql3 = "select replicate(string3, numeric3) from source";
TransformConfig config3 = new TransformConfig(transformSql3);
TransformProcessor<String, String> processor3 = TransformProcessor
.create(config3, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
List<String> output4 = processor3.transform("apple|banana|cloud|2|1|0", new HashMap<>());
Assert.assertEquals(1, output4.size());
Assert.assertEquals(output4.get(0), "result=");
// case7: replicate('cloud', 0)
List<String> output7 = processor3.transform("apple|banana|cloud|2|1|0", new HashMap<>());
Assert.assertEquals(1, output7.size());
Assert.assertEquals(output7.get(0), "result=");
// case8: replicate('banana', 2)
List<String> output8 = processor2.transform("apple|cloud|banana|1|2|3", new HashMap<>());
Assert.assertEquals(1, output8.size());
Assert.assertEquals(output8.get(0), "result=cloudcloud");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({TestInsertFunction.class, TestLeftFunction.class, TestLengthFunction.class,
TestLocateFunction.class, TestLowerFunction.class, TestLpadFunction.class, TestReplaceFunction.class,
TestReplicateFunction.class, TestReverseFunction.class, TestRightFunction.class, TestRpadFunction.class,
TestRepeatFunction.class, TestReverseFunction.class, TestRightFunction.class, TestRpadFunction.class,
TestSpaceFunction.class, TestStrcmpFunction.class, TestSubstringFunction.class, TestToBase64Function.class,
TestTranslateFunction.class, TestTrimFunction.class, TestUpperFunction.class, TestContainsFunction.class,
TestFromBase64Function.class
Expand Down

0 comments on commit 1921b5e

Please sign in to comment.